Skip to content Skip to sidebar Skip to footer

Why Does The SetTimeout Function Execute Forever?

I read about the timing event. I am confused about setTimeout: I have an example on W3Schools where it is running every 500 milliseconds. I read about setTimeout that it works only

Solution 1:

The function resets the timer internally:

setTimeout(function(){startTime()},500);

Every time it runs, the last thing it does is schedule another iteration.


Solution 2:

setTimeout function works only once. Look into your code. Inside the startTime function , you are calling the same function again in 500 ms. If you want it to work only once, dont call it recursively. Or if you want it to repeat for sometime, use setInterval instead. This function returns a id using which you can stop it whenever you want.


Post a Comment for "Why Does The SetTimeout Function Execute Forever?"