This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _uuid() { | |
var d = Date.now(); | |
if (typeof performance !== 'undefined' && typeof performance.now === 'function'){ | |
d += performance.now(); //use high-precision timer if available | |
} | |
return '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); | |
}); | |
} |
如果只需兼容現代瀏覽器(Chrome)的話, 可以更簡易地使用…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function uuid2() { | |
return window.crypto.randomUUID(); | |
} |
目前應該只有內核為Chromium的瀏覽器有支援到, 就是Chrome, Edge等等, 其他瀏覽器的支援程度可以看看這邊.