基于时间戳生成20位全局唯一标识(每一毫秒只对应一个唯一的标识,适用于生成DOM节点ID)

发表于 2016-11-23 17:02:30   |   下载附件   |   字体:
随机ID生成randID
/* 
 * 用途:基于时间戳生成20位全局唯一标识(每一毫秒只对应一个唯一的标识,适用于生成DOM节点ID) 
 */
HR.tool.randID = function(len) {
    var timestamp = new Date().getTime() || 0,
        chars = 'abcdefghijklmnopqrstuvwxyz',
        uuid = '';
    this.timestamp = this.timestamp == timestamp ? timestamp + 1 : timestamp;
    timestamp = '' + this.timestamp;
    len = len || 20;
    for (var i = 0; i < len; i++) {
        var k = timestamp.charAt(i);
        if (k == '') {
            k = Math.floor(Math.random() * 26);
        }
        uuid += chars.charAt(k) || 'x';
    }
    return uuid;
};