Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems in Javascript Timer

 
Ashwin Sridhar
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am learning timers in Java script. I am able to get my timer work correctly, when the setInterval code is within a function that is being called by the onload event of the body. But the same doesn't work if the setInterval is within a function called by the button on its click.



Could someone help me understand the above.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64856
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a string for the timer callback is a poor practice. The code should be:

And, define "doesn't work".

Have you set a breakpoint in the timer to see if it's being called? If not, is the handler correctly called? Are there any errors in the console?
 
Ashwin Sridhar
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear ,

Have you set a breakpoint in the timer to see if it's being called?


I am not sure how to set breakpoint but I used alert to check if function is being called. The callTimer2() is not getting called when I invoke setInterval() within the function stopWatch().

Are there any errors in the console?


I have my scripts on a .html file and just run it on a browser. So I dont get anything on console.

Please use proper indentation when posting code.


I used the code tag for posting the code. This is the way I do for Java codes. Is there something else that I need to follow ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64856
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Sridhar wrote:I am not sure how to set breakpoint but I used alert to check if function is being called.

Alerts are useless as a debugging tool -- especially for time-sensitive issues like timers. Every modern browser has a built-in debugger (or a plugin). Learn to use it. Otherwise, you're mostly guessing.

Are there any errors in the console?


I have my scripts on a .html file and just run it on a browser. So I dont get anything on console.

On the JavaScript console? Do you even have the console open?

I used the code tag for posting the code. This is the way I do for Java codes. Is there something else that I need to follow ?

Yes. Proper indentation. Do you not indent your code?
 
Ashwin Sridhar
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alerts are useless as a debugging tool -- especially for time-sensitive issues like timers. Every modern browser has a built-in debugger (or a plugin). Learn to use it. Otherwise, you're mostly guessing.

On the JavaScript console? Do you even have the console open?



Just started using FireBug. I don't find any errors on the console. The timer is working when its called by the onload event but its not working when called by the click event. As simple as that. Any idea why such thing happens Bear.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64856
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't want to use breakpoints, you can use console.log() statements to log to the console.

And please re-post the code with proper indentation so I can see if there are any glaring errors.
 
Ashwin Sridhar
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Let me know if this helps you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic