Create a unique number with javascript time

后端 未结 30 3120
生来不讨喜
生来不讨喜 2020-12-02 08:31

I need to generate unique id numbers on the fly using javascript. In the past, I\'ve done this by creating a number using time. The number would be made up of the four digi

30条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-02 08:48

    function getUniqueNumber() {
    
        function shuffle(str) {
            var a = str.split("");
            var n = a.length;
            for(var i = n - 1; i > 0; i--) {
                var j = Math.floor(Math.random() * (i + 1));
                var tmp = a[i];
                a[i] = a[j];
                a[j] = tmp;
            }
            return a.join("");
        }
        var str = new Date().getTime() + (Math.random()*999 +1000).toFixed() //string
        return Number.parseInt(shuffle(str));   
    }
    

提交回复
热议问题