Is memory timed out?

This question is probably trivial, but I'm confused as to what I should be doing.

Basically, I noticed that every subsequent timeout I call has a larger ID. I'm sure this is just to reduce ID conflicts, but I'm worried that although the timeout has already occurred, it still exists in memory.

If the timeout function has been executed, do you need to call something to "remove" it? In my code, I am doing this:

clearTimeout(time);// destroy previous timer?
time = setTimeout( function(){ hitup( towards );}, ms);// next execution

      

+3


source to share


3 answers


How exactly the Javascript engine implements the timer value store by time is entirely implementation dependent. They are probably using some sort of map or hash table, as this is the classic storage mechanism for sparse (non-sequential) values ​​like this.

Previously fired or cleared timers will not stay active or use memory. You don't need to name anything to remove the timer.

The code you showed:



clearTimeout(time);// destroy previous timer?
time = setTimeout( function(){ hitup( towards );}, ms);// next execution

      

sometimes still useful. If you don't already know if the timer store was actually stored in a variable time

and you no longer want to run it and you are about to overwrite the variable where you store your ID, then it sometimes makes sense to call clearTimeout()

in this case. If you know the timer has fired or if you want to start it then there is no need to call clearTimeout()

on it.

The system will take care of memory management here. This is not something you need to worry about. You only need to worry about stopping the timer if it's not already fired and you don't want it to fire at the appointed time.

+6


source


Dry, if the timeout function is executed, it and its closure will be deleted. TimeoutID autoincrement is used only to avoid conflicts.

But in some cases, a closure of the timeout function may exist if it is used elsewhere - for example, if you create another timeout function or event handler, or simply refer to the global object.



You don't need to call special delete functions.

+1


source


If you are not using a variable time

for anything, then do not assign a timeout to the variable.

If you don't assign setTimeout to a variable, you can use timers indefinitely without wasting memory if they expire. for example many timeouts with long times will use memory before they expire.

0


source







All Articles