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 ...

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.