This question is probably trivial, but I am confused about what I should do.
Basically, I noticed that each successive timeout I call has a greater ID number. I'm sure this is just to reduce ID conflicts, but I'm worried that even though a timeout has already executed, it still exists in memory.
If a timeout function has been executed, do you need to call anything to "delete" it? In my code I'm doing this:
clearTimeout(time);// destroy previous timer?
time = setTimeout( function(){ hitup( towards );}, ms);// next execution
This question is probably trivial, but I am confused about what I should do.
Basically, I noticed that each successive timeout I call has a greater ID number. I'm sure this is just to reduce ID conflicts, but I'm worried that even though a timeout has already executed, it still exists in memory.
If a timeout function has been executed, do you need to call anything to "delete" it? In my code I'm doing this:
clearTimeout(time);// destroy previous timer?
time = setTimeout( function(){ hitup( towards );}, ms);// next execution
Share
Improve this question
edited Apr 26, 2015 at 17:26
Reg Edit
6,9342 gold badges41 silver badges48 bronze badges
asked Apr 26, 2015 at 17:19
AndrewAndrew
3,6734 gold badges28 silver badges46 bronze badges
2
- 1 clearTimeout() is only needed to stop the event from happening in the future. – Dave Jones Commented Apr 26, 2015 at 17:25
- 1 You'd need 9007199254740991 timeouts in order to cause any conflict. – Benjamin Gruenbaum Commented Apr 26, 2015 at 18:27
3 Answers
Reset to default 8How exactly a Javascript engine implements timerID value storage is entirely implementation dependent. They probably uses some sort of map or hashtable since that's a classic storage mechanism for sparse (non-sequential) values like this.
Previously fired or cleared timers will not still be active or using memory. You do not need to call anything to delete a timer.
The code you showed:
clearTimeout(time);// destroy previous timer?
time = setTimeout( function(){ hitup( towards );}, ms);// next execution
is sometimes still useful. If you don't yet know if the timer store in the variable time
has actually fired and you do not want it to fire anymore and you're about to overwrite the variable where you store its ID, then it does sometimes make sense to call clearTimeout()
in that case. If you know the timer has fired or if you want it to fire, then there is no need to call clearTimeout()
on it.
The system will take care of memory management here. That is not something you have to worry about. You only have to worry about stopping a timer if it hasn't yet fired and you don't want it to fire at its scheduled time.
Ususally if the timeout function executed, it and it's closure will be deleted. TimeoutID autoincrement is used only for avoiding conflicts.
But in some cases the closure of timeout function can exist if it used somewere else - for example if you create another timeout function or event handler or just make reference to global object.
You don't need to call any special delete function.
If you are not using the time
variable for anything don't assign the timeout to a variable.
If you don't assign setTimeout to a variable, you can use timers indefinitely without wasting memory provided they are expirying. e.g. many timeouts with long times will use memory until they expire.