jeudi 18 juin 2020

Generate unique name/ id (time based?/ random based?) using JavaScript [duplicate]

I'm trying to generate random names in a loop. Currently I'm using:

let name = `Name_{ (new Date()).getTime() }`;

But to my surprise, the method getTime() return the same value for 2 different loop iterations. See for your self (run snippet)

for (let i = 0; i < 20; i++) {
  console.log((new Date()).getTime())
}

I was thinking to use some random() function, but how can't be sure the random() function won't generate the same value twice?




Aucun commentaire:

Enregistrer un commentaire