导航栏: 首页 评论列表

JS生成uniqueID的讨论

默认分类 2011-11-13 20:33:14

JS生成uniqueID

方式1 随机数

uniqueID = Math.floor(Math.random()*10000000000)+"";

方式2 时间戳+随机数

uniqueID = (new Date()).getTime()+''+Math.floor(Math.random()*100000)+control.id

探讨的问题: 1ms 能执行多少次(new Date()).getTime()

测试代码:

function a(){ var i,k,t1,t2,t3,t4=1000000; for(k=0;k<100;k++){ t1 = new Date(); for(i=0;i<10000;i++){ k = (new Date()).getTime(); } t2 = new Date(); t3 = t2.getTime()-t1.getTime(); if(t3<t4) t4=t3; } alert(t4); }

测试结果 IE9/FF8~300ms;chrome7~70ms


>> 留言评论