aspose file tools
The moose likes HTML, CSS and JavaScript and the fly likes Problems in Javascript Timer Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "Problems in Javascript Timer" Watch "Problems in Javascript Timer" New topic
Author

Problems in Javascript Timer

Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

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.


Ashwin Sridhar
SCJP | SCWCD | OCA
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56191
    
  13

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?


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

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

Joined: Jan 10, 2002
Posts: 56191
    
  13

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

Joined: Jul 09, 2011
Posts: 272


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

Joined: Jan 10, 2002
Posts: 56191
    
  13

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

Joined: Jul 09, 2011
Posts: 272



Let me know if this helps you.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Problems in Javascript Timer
 
Similar Threads
running servlet after the browser is closed
Countdown Timer or JavaScript replacing HTML
How to access content of p tag
how to create timer in jsp?
document[].src returning null