How are the timeout IDs allocated? #261
Replies: 10 comments
-
Posted at 2014-04-22 by @gfwilliams I'm not sure. I had thought the assignment was:
However I had to write some code to cope with timers being added/removed while handling a timer, and it could be that does some strange things with the numbering... |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-04-22 by graf OK so what is the maximum ID this function can return? Is it some huge number in tens of thousands? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-04-22 by graf Turns out If I add another watch for And that's just the ID's value growing, the number of timers is the same as far as I understand. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-04-23 by @gfwilliams The ID can be up to 18446744073709551615 - so I think you're safe! That may change to 4294967296 at some point in the future, but even so I think you'll be fine! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-04-23 by graf Lol now that's reassuring. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-04-25 by graf One more question because this is driving me nuts and I can't find the reason why What happens when I Also when I add The output now looks like that:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-04-28 by @gfwilliams When you say Surely the increased memory usage is because there is already a timer allocated, as you hadn't yet freed it with clearTimeout? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-04-28 by graf
And this is that part that I don't understand :) I AM doing clearTimeout and led behaves like it was removed (timeout is reset) but memory usage and ID numbering suggests otherwise. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-04-28 by @gfwilliams Surely you're just dumping the memory usage before you remove the timer?
Try this:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-04-28 by graf
Ahhh good catch, thanks! But it was also before I add another timer.. EDIT: That's it! On the first console.log the memory usage was lower because timer was not even set at this point. Then on the second console.log the timer was finally set and memory usage was higher. Thanks for helping me understand this :) Now it's only the ID numbering that puzzles me. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2014-04-22 by graf
Could someone please explain me how are the IDs allocated? Or even better why it's not like I thought? :)
In this code below I would expect the
ledTimer
to get the same value (ID) over and over again but that doesn't happen.Now when I press the button every 5 seconds I get what I expected:
But when I press the button every 3 seconds then it's not what I expected:
Beta Was this translation helpful? Give feedback.
All reactions