4

Are the IDs returned by setTimeout and setInterval ever recycled after they have completed (or have been cleared)? Or are all IDs unique for the life of the session?

Could calling clearTimeout or clearInterval on an old ID cause an unrelated timeout/interval to be cleared?

Acorn
  • 49,061
  • 27
  • 133
  • 172

1 Answers1

5

From the spec

Each object that implements the WindowTimers interface has a list of active timers. Each entry in this lists is identified by a number, which must be unique within the list for the lifetime of the object that implements the WindowTimers interface.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • The actual answer to the question isn't very clear to me in this answer, since I don't know what exactly is meant by "the lifetime of the object that implements the WindowTimers interface". If that means the `window` object, I guess that means the answer is "no, they're not recycled in that browser tab until navigation". But [this answer](https://stackoverflow.com/a/53127712/496046) shows some Blink internals, and if I'm reading it correctly, it *does* reuse timer IDs once the page has used about 2^31 timers. So I guess the browsers aren't following the spec and the real answer is "yes". – tremby Sep 13 '19 at 22:38
  • I don't see how that's helpful. If that's relevant to the answer, then mention it in the answer, otherwise the answer isn't complete. But to answer *your* question: I don't know exactly what the libraries I'm using are doing, and I don't know how long the page will be open. So the answer is maybe. – tremby Sep 15 '19 at 05:57