This one has me boggled. I have a timer on a page that increments every second. If a certain task completes the timer is cleared and based on a bunch of checks is either restarted or not. In my code I clearTimeout on the Timer variable and no where after that do I restart the timer but the function used when initializing the timer continues to keep getting hit even after a clearTimeout call. Anyone able to think of any possible reasons why? I thought clearTimeout would do the trick. Thanks in advance for any help.
It was with setTimeout. Found the problem but it raises a couple of additional questions. If the timer is called in a recursive way (i.e. the function inside the timer call is a call to itself every second) would the clearTimeout immediately kill the Timer? If there are multiple calls using the same Timer variable shouldn't clearTimeout clear out all pending calls? This is what it appears as though was happening. There were 2 places a call was happening in regards to the Timer object. Both separate calls to the setTimeout were set to 1 second. Once I reworked the code a little and was able to remove the first call everything worked fine. Thanks for the response.
No, they are not named. The variable holds a reference to the timer. When you reuse it, you override the previous one.
If you need multiple timers, hold them in an array.
Than you can loop it when you need to kill them.
Joined: Apr 09, 2002
No I didn't need multiple timers and was hoping/thinking/etc.., it would override the variable but the weird scenario I was getting almost suggested otherwise. It was as though if you made 2 calls to the timer function (that calls itself) and then tried to clearTimeout on the variable there would still be a call on the stack that would keep the timer going, as though the clearTimeout only removed 1 call pending on the stack. The recursive calls were set to 1 second apart and the code being run was situated in a few different functions. I changed the code slightly so as to remove the possibility of the second call and it seems to have worked and passed initial testing. Thans for the reply.
Joined: Nov 08, 2001
so basically what you have done is this
Joined: Apr 09, 2002
"what I have done" is more like "what I'm trying to fix" :-)
Roughly here is simpolified version :
function1 and function2 got called and then awhile later (based on a button click) processingFunction gets called (with no code following) but the somefunction function was still being called.