We help IT Professionals succeed at work.

Javascript - Timer to call button

carsRST
carsRST asked
on
How do I create a timer within Javascript that will repeatedly call a javascript function every 120 seconds?
Comment
Watch Question

Top Expert 2011
Commented:
window.setInterval("functionYouWantToCall()",12000);

Open in new window

Author

Commented:
Thanks!
Philippe DamervalSenior Analyst Programmer

Commented:
CarsRST,
Javascript is single-threaded - which means it executes in linear fashion, first call to last call, and while you can delay the execution of a function or repeat a function call indefinitely at regular interval, there can be no true timer in Javascript. At best, you can have a function that is called again after a specific period of time. This post presents the mechanism very well:
http://ejohn.org/blog/how-javascript-timers-work/
However, you can cleverly set things up so that your function is sensitive to the current time, emulating a fairly acceptable version of a timer. This post gives you a good sample script for doing that:
http://mcfedries.com/javascript/timer.asp

Hope this helps,

Philippe