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-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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 so low that it can be ignored. * Disclaimer: This code is exclusive to the author's learning. If the user suffers losses due to code problems during use, it has nothing to do with the author. * Date: September 4, 2014 * Author: wyc */ function GUID() { = new Date(); /* Determine whether the following code has been initialized. If the following code has been initialized, the following code will no longer be executed, and it will only be executed once in reality */ if (typeof != 'function') { /* Generate GUID code */ = function() { = new Date(); var guidStr = ''; sexadecimalDate = ((), 16); sexadecimalTime = ((), 16); for (var i = 0; i < 9; i++) { guidStr += (()*16).toString(16); } guidStr += sexadecimalDate; guidStr += sexadecimalTime; while( < 32) { guidStr += (()*16).toString(16); } return (guidStr); } /* * Function: Get the GUID format of the current date, that is, the date of 8 digits: 19700101 * Return value: Return the string of string in GUID date format */ = function() { return () + (() + 1) + (()); } /* * Function: Get the GUID format of the current time, that is, the time of 8 digits, including milliseconds, and milliseconds are 2 digits: 12300933 * Return value: Return the string of string in GUID date format */ = function() { return (()) + (()) + (()) + ( parseInt(() / 10 )); } /* * Function: Add 0 to a positive integer of a single digit. If it is a string that can be converted into a non-NaN number, it can also be implemented. * Parameters: Parameters indicate that the number 0 is prepared or a string that can be converted into a number. * Return value: If the condition meets the criteria, return the string type after adding 0, otherwise return its own string. */ = function(num) { if (Number(num).toString() != 'NaN' && num >= 0 && num < 10) { return '0' + (num); } else { return (); } } /* * Function: Convert y-digit values to x-digit values * Parameters: The first parameter represents the value to be converted; the second parameter represents the binary system to be converted; the third parameter is optional, indicating the current binary number, if not written, it is 10 * Return value: Return the converted string */ = function(num, x, y) { if (y != undefined) { return parseInt((), y).toString(x); } else { return parseInt(()).toString(x); } } /* * Function: Format 32-bit strings as strings in GUID mode * Parameter: The first parameter represents a 32-bit string * Return value: String in standard GUID format */ = function(guidStr) { var str1 = (0, 8) + '-', str2 = (8, 12) + '-', str3 = (12, 16) + '-', str4 = (16, 20) + '-', str5 = (20); return str1 + str2 + str3 + str4 + str5; } } }
GUID Object
Just save it in a JS file and reference it.
Then we just need it.
var guid = new GUID();
alert(());
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.
The above is the method of generating GUID written by me. If there is a better method, please tell me, thank you!