摘要:
下文讲述javascript中生成类似UUID格式的唯一标识符的方法分享,如下所示:
UUID简介:
UUID是唯一标识列,应用非常广泛
UUID理论上总数为 3.4*10^38 如果每纳秒生成1兆个uuid,需100亿年才能用完所有的uuid,即UUID可以理解为不重复的标识信息列
UUID是由一组32个16进制数字所组成
javascript 生成UUID 举例说明
function jsBuildUUID() {
var d = new Date().getTime();
if (window.performance && typeof window.performance.now === "function") {
d += performance.now(); //use high-precision timer if available
}
var uid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return uid;
}
document.write(jsBuildUUID() +"
"+jsBuildUUID() +"
"+jsBuildUUID() +"
");
javascript_js生成UUID_GUID方法分享