GUID (Global Unified Identifier) refers to the number generated on a machine, which ensures that it is unique to all machines in the same space and time. Usually the platform provides an API for generating GUIDs. The generation algorithm is very interesting, using Ethernet card address, nanosecond time, chip ID code and many possible numbers. The only drawback of GUID is that the result string generated will be relatively large.
The format of GUID is: xxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Everyone knows that GUID is not very useful in front-end development, but if you need to insert an ID and this ID corresponds to the background, etc., we can still generate a GUID for convenience.
It is generally very simple to generate GUIDs in background or database languages such as sql, java, C#, etc., but the front-end does not have a method to generate GUIDs directly, so you can only write one by yourself. However, because the GUID needs to obtain the address of the Ethernet card and the time in nanoseconds. It is difficult to obtain this information from the front end (please tell me if you know about children's shoes), and we can simulate the implementation and generate the GUID, the code is as follows:
/*
* Function: Generate a GUID code, where the GUID is composed of less than 14 dates and times and more than 18 hexadecimal random numbers. GUID has a certain repetition probability, but the repetition probability is extremely low. Theoretically, the repetition probability is 1/(16^18) per 10ms, that is, 1 part to the power of 16, and the repetition probability is as low as negligible*/
function GUID() {this.date = new Date();/* Determine whether it has been initialized. If the following code is initialized, the following code will no longer be executed. In fact, it will only be executed once */if (typeof this.newGUID != 'function') {/* Generate GUID code*/GUID.prototype.newGUID = function() {this.date = new Date();var guidStr = '';sexadecimalDate = this.hexadecimal(this.getGUIDDate(), 16);sexadecimalTime = this.hexadecimal(this.getGUIDTime(), 16);for (var i = 0; i < 9; i++) {guidStr += Math.floor(Math.random()*16).toString(16);}guidStr += sexadecimalDate;guidStr += sexadecimalTime; while(guidStr.length < 32) {guidStr += Math.floor(Math.random()*16).toString(16);} return this.formatGUID(guidStr);}/** Function: Get the GUID format of the current date, that is, the date of the 8-digit number: 19700101* Return value: Return the string of the GUID date format */GUID.prototype.getGUIDDate = function() {return this.date.getFullYear() + this.addZero(this.date.getMonth() + 1) + this.addZero(this.date.getDay());}/** Function: Get the GUID format of the current time, that is, the 8-digit time, including milliseconds, milliseconds are 2-digit: 12300933* Return value: Return the string of the GUID date format */GUID.prototype.getGUIDTime = function() {return this.addZero(this.date.getHours()) + this.addZero(this.date.getMinutes()) + this.addZero(this.date.getSeconds()) + this.addZero(parseInt(this.date.getMilliseconds() / 10 ));}/** Function: Add 0 to a positive integer of a single digit. If it is a string that can be converted to a non-NaN number, it can also be implemented. Parameters: Parameters indicate that a number that is prepared to be added before or a string that can be converted into a number* Return value: If the condition is met, return the string type after adding 0, otherwise it will return its own string*/GUID.prototype.addZero = function(num) {if (Number(num).toString() != 'NaN' && num >= 0 && num < 10) {return '0' + Math.floor(num);} else {return num.toString();}}/* * Function: Convert the y-digit value to x-digit value* Parameters: The first parameter represents the value to be converted; the second parameter represents the particle to be converted; the third parameter is optional, representing the current particle number, if not written, it is 10* Return value: Return the converted string*/GUID.prototype.hexadecimal = function(num, x, y) {if (y != undefined) {return parseInt(num.toString(), y).toString(x);} else {return parseInt(num.toString()).toString(x);}}/** Function: Format the 32-bit string to a string in GUID mode* Parameters: The first parameter represents a 32-bit string* Return value: String in standard GUID format*/GUID.prototype.formatGUID = function(guidStr) {var str1 = guidStr.slice(0, 8) + '-',str2 = guidStr.slice(8, 12) + '-',str3 = guidStr.slice(12, 16) + '-',str4 = guidStr.slice(16, 20) + '-',str5 = guidStr.slice(20);return str1 + str2 + str3 + str4 + str5;}}}GUID Object
Just save it in a JS file and reference it.
Then we just need
var guid = new GUID();
alert(guid.newGUID());
You can get the GUID code.
The implementation principle is very simple. Here we just use system time and more than 18 hexadecimal random numbers, and convert system time to hexadecimal. Although it is still possible to repeat, the probability of repetition is extremely low and can be ignored.