aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Timers in javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Timers in javascript" Watch "Timers in javascript" New topic
Author

Timers in javascript

Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 823
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

How are you establishing the timer? With setTimeout() or setInterval()? If the latter, you need to use clearInterval().


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 823
Hey Bear,
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.

RH
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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.
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 823
Hey Eric,
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.

Rob
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
so basically what you have done is this



Eric
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 823
"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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Timers in javascript