O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding a Recurring Timer

var statusTimerId; var status = "OK"; //checks status every minute as long as it is "OK" function checkStatus () {   if(status == "OK"){     alert("Status OK");     statusTimerId = setInterval(checkStatus, 60000);   } else {     alert("Status Failed");   } } statusTimerId = setInterval(checkStatus, 60000);

You can also start a timer that triggers on a regular interval using the setInterval(function, ms) method. This method also accepts a function name and milliseconds as arguments. Inside the function, you need to call set interval again on the same function so that the code will be called again.

To turn off the recurring timer, simply do not call setInterval() again inside the timer ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required