Javascript UUID 博客分类: JavaScript jsuuid
程序员文章站
2024-03-24 13:25:22
...
代码来自“美篇”。
function UUID(){ this.id = this.createUUID(); } UUID.prototype.valueOf = function(){ return this.id; }; UUID.prototype.toString = function(){ return this.id; }; UUID.prototype.createUUID = function(){ var dg = new Date(1582, 10, 15, 0, 0, 0, 0); var dc = new Date(); var t = dc.getTime() - dg.getTime(); var tl = UUID.getIntegerBits(t,0,31); var tm = UUID.getIntegerBits(t,32,47); var thv = UUID.getIntegerBits(t,48,59) + '1'; // version 1, security version is 2 var csar = UUID.getIntegerBits(UUID.rand(4095),0,7); var csl = UUID.getIntegerBits(UUID.rand(4095),0,7); var n = UUID.getIntegerBits(UUID.rand(8191),0,7) + UUID.getIntegerBits(UUID.rand(8191),8,15) + UUID.getIntegerBits(UUID.rand(8191),0,7) + UUID.getIntegerBits(UUID.rand(8191),8,15) + UUID.getIntegerBits(UUID.rand(8191),0,15); // this last number is two octets long return tl + tm + thv + csar + csl + n; }; UUID.getIntegerBits = function(val,start,end){ var base16 = UUID.returnBase(val,16); var quadArray = []; var quadString = ''; for(i=0;i< base16.length;i++){ quadArray.push(base16.substring(i,i+1)); } for(i=Math.floor(start/4);i<=Math.floor(end/4);i++){ if(!quadArray[i] || quadArray[i] == '') quadString += '0'; else quadString += quadArray[i]; } return quadString; }; UUID.returnBase = function(number, base){ return (number).toString(base).toUpperCase(); }; UUID.rand = function(max){ return Math.floor(Math.random() * (max + 1)); };
推荐阅读
-
Javascript UUID 博客分类: JavaScript jsuuid
-
JS onkeydown onenter 博客分类: 编程相关Html Css Js JavaScript
-
常见验证问题js 博客分类: javascript EXTQQMobile音乐
-
clipboard.js 分析与研究 可用html页面复制信息到剪切板 博客分类: JavaScript JavaScript
-
javascript 将时间字符串转换为时间格式 博客分类: javascript javascript日期转换date
-
理解闭包 博客分类: javascript 闭包js
-
js操作select 博客分类: Javascript javascriptfirefoxhtmloperaie
-
js计算字符数 博客分类: Javascript BorlandGo
-
js计算字符数 博客分类: Javascript BorlandGo
-
定制 jquery 插件 博客分类: javascript jQuery