Skip to content

Instantly share code, notes, and snippets.

@monjer
Last active December 20, 2015 10:09
Show Gist options
  • Select an option

  • Save monjer/6113637 to your computer and use it in GitHub Desktop.

Select an option

Save monjer/6113637 to your computer and use it in GitHub Desktop.
(function(opt_ns,opt_win){
var ALPHA_CHAR_CODES = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70];
// var uuidBuffer = [];
var uuidFactory = {
/**
* @return {String} 新生成的uuid字符串
*/
createUUID:function(){
var uid= new Array(36);
var index = 0;
var i , j;
for (i = 0; i < 8; i++) {
uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() * 16)];
}
for (i = 0; i < 3; i++) {
uid[index++] = 45; // charCode for "-"
for (j = 0; j < 4; j++) {
uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() * 16)];
}
}
uid[index++] = 45; // charCode for "-"
var time = new Date().getTime();
var timeString = ("0000000" + time.toString(16).toUpperCase()).substr(-8);
for (i = 0; i < 8; i++) {
uid[index++] = timeString.charCodeAt(i);
}
for (i = 0; i < 4; i++) {
uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() * 16)];
}
var uuid = String.fromCharCode.apply(null, uid);
// uuidBuffer.push(uuid);
return uuid;
}
// isUUIDExisted:function(uuid){
// var length = uuidBuffer.length ;
// for(var i = 0 ; i < length ; i++){
// if (uuid === uuidBuffer[i] ) {
// return true ;
// }
// }
// return false ;
// }
}
opt_win = opt_win || top ;
opt_ns = opt_ns || opt_win;
opt_ns.uuidFactory = uuidFactory ;
/**
* 一种简化处理 http://note19.com/2007/05/27/javascript-guid-generator/
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
**/
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment