I am a WordPress/PHP/JS developer, working as a web engineer for a proptech company in Dublin, Ireland. I have my side projects and I like to experiment with various web technologies.
Depending on the application, this is the right way to do it. In my case, I don't have a loop, I have several elements being displayed on subsequent clicks, and there's several seconds between each click. So there's zero chances of an identical ID.
You could decrease the chances by using multiple of the author's ways:
Maybe
function uniqueID() {
return Math.floor(Math.random() * Date.now())
}
You could even string multiple randoms together:
Math.floor(Math.random() * Math.floor(Math.random() * Date.now()))
Depending on the application, this is the right way to do it. In my case, I don't have a loop, I have several elements being displayed on subsequent clicks, and there's several seconds between each click. So there's zero chances of an identical ID.
thank you, this work also. to also include string i use Math.floor(Math.random() * Date.now()).toString(16)