สันนิษฐานว่าโซลูชันที่เสนอโดย@abarberเป็นทางออกที่ดีเนื่องจากการใช้งาน(new Date()).getTime()จึงมีช่วงเวลามิลลิวินาทีและผลรวมtickในกรณีที่เกิดการชนกันในช่วงเวลานี้เราสามารถพิจารณาใช้บิวท์อินได้ตามที่เห็นได้ชัดเจนในการดำเนินการที่นี่:
เราสามารถดูได้ที่นี่ว่าจะมีการชนกันในกรอบหน้าต่าง 1/1000 ได้อย่างไรโดยใช้(new Date()).getTime():
console.log( (new Date()).getTime() ); console.log( (new Date()).getTime() )
VM1155:1 1469615396590
VM1155:1 1469615396591
console.log( (new Date()).getTime() ); console.log( (new Date()).getTime() )
VM1156:1 1469615398845
VM1156:1 1469615398846
console.log( (new Date()).getTime() ); console.log( (new Date()).getTime() )
VM1158:1 1469615403045
VM1158:1 1469615403045
ประการที่สองเราลองใช้โซลูชันที่เสนอเพื่อหลีกเลี่ยงการชนกันในหน้าต่าง 1/1000:
console.log( window.mwUnique.getUniqueID() ); console.log( window.mwUnique.getUniqueID() ); 
VM1159:1 14696154132130
VM1159:1 14696154132131
ที่กล่าวว่าเราสามารถพิจารณาใช้ฟังก์ชันเช่นโหนดprocess.nextTickที่เรียกในลูปเหตุการณ์เป็นแบบเดี่ยวtickและอธิบายได้ดีที่นี่ แน่นอนว่าไม่มีในเบราว์เซอร์process.nextTickดังนั้นเราต้องคิดว่าจะทำอย่างไร
นี้การดำเนินการจะติดตั้งnextTickฟังก์ชั่นในเบราว์เซอร์โดยใช้ฟังก์ชั่นที่ใกล้ชิดที่สุดในการ I / O ในเบราว์เซอร์ที่มีsetTimeout(fnc,0), ,setImmediate(fnc) window.requestAnimationFrameตามที่แนะนำไว้ที่นี่เราสามารถเพิ่มได้window.postMessageแต่ฉันทิ้งสิ่งนี้ไว้ให้ผู้อ่านเนื่องจากมันต้องการaddEventListenerเช่นกัน ฉันได้แก้ไขเวอร์ชันโมดูลดั้งเดิมเพื่อให้ง่ายขึ้นที่นี่:
getUniqueID = (c => {
 if(typeof(nextTick)=='undefined')
nextTick = (function(window, prefixes, i, p, fnc) {
    while (!fnc && i < prefixes.length) {
        fnc = window[prefixes[i++] + 'equestAnimationFrame'];
    }
    return (fnc && fnc.bind(window)) || window.setImmediate || function(fnc) {window.setTimeout(fnc, 0);};
})(window, 'r webkitR mozR msR oR'.split(' '), 0);
 nextTick(() => {
   return c( (new Date()).getTime() )  
 })
})
ดังนั้นเราจึงมีในหน้าต่าง 1/1000:
getUniqueID(function(c) { console.log(c); });getUniqueID(function(c) { console.log(c); });
undefined
VM1160:1 1469615416965
VM1160:1 1469615416966