mcya / JavaScriptExperience

👮 JavaScript Experience for web(JavaScript归类:简单入门+数组+经典循环+字符串+函数+Date日期+简单对象+BOM+DOM) +[ Issues(js实用技巧)]
29 stars 8 forks source link

js 生成唯一id uuid #47

Open mcya opened 6 years ago

mcya commented 6 years ago

// 生成唯一id 主要是为了能在前台自助操控数据 - 即根据ID去判断数据更改对应数据

getUuid() {
  var nowDate = new Date().getTime();
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = (nowDate + Math.random()*16)%16 | 0;
    nowDate = Math.floor(nowDate/16);
    return (c=='x' ? r : (r&0x3|0x8)).toString(16);
  });
  return uuid;
}