Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Timers in javascript

 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you establishing the timer? With setTimeout() or setInterval()? If the latter, you need to use clearInterval().
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so basically what you have done is this



Eric
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic