Javascript – Id for setInterval. How does it work

javascript

Suppose I have an array that stores all my ids used for setInterval.

What if I want to create an id, dynamically… can I do like this?

id_array.push(++id_var) = setInterval(function, milliseconds);

See?

I also have a variable, called 'id_var', that I believe, if I increment it, it will give me, a new 'id'.

Is this code correct?

Best Answer

The return value from setInterval() is an opaque token. You just store it somewhere until you want to remove the interval timer with clearInterval().

You cannot control the value of what you receive, nor can you do any kind of arithmetic on it. Just store it and retrieve it.