一:
export const uuid = () => {
const temp_url = URL.createObjectURL(new Blob())
const uuid = temp_url.toString()
URL.revokeObjectURL(temp_url) //释放这个url
return uuid.substring(uuid.lastIndexOf('/') + 1)
}
uuid() // a640be34-689f-4b98-be77-e3972f9bffdd
二、NanoID
let urlAlphabet =
'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
let nanoid = (size = 21) => {
let id = ''
// A compact alternative for `for (var i = 0; i < step; i++)`.
let i = size
while (i--) {
// `| 0` is more compact and faster than `Math.floor()`.
id += urlAlphabet[(Math.random() * 64) | 0]
}
return id
}
//结果示例:AfRTJv9hRo42vKKUDBQLX
三、使用随机数
const uniqueId = 'id-' + Math.random().toString(36).substr(2, 9);
console.log(uniqueId);
四、基于时间戳和随机数
const uniqueId = 'id-' + new Date().getTime().toString(36) + '-' + Math.random().toString(36).substr(2, 9);
console.log(uniqueId);
五、基于性能计数器
const uniqueId = 'id-' + window.performance.now().toString().replace('.', '');
console.log(uniqueId);