Create a unique number with javascript time

后端 未结 30 3118
生来不讨喜
生来不讨喜 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:45

    In 2020, you can use the in-browser Crypto API to generate cryptographically strong random values.

    function getRandomNumbers() {
      const typedArray = new Uint8Array(10);
      const randomValues = window.crypto.getRandomValues(typedArray);
      return randomValues.join('');
    }
    
    console.log(getRandomNumbers());
    // 1857488137147725264738
    
    

    both Uint8Array and Crypto.getRandomValues are supported on all major browsers, including IE11

提交回复
热议问题