// Keep saying hello, infinitely, every 5 seconds:
var timer = setInterval( "alert('Hello!')", 5000 );
setInterval( "doSomethingNeat()", 12345 );
var counter = 0;
var login = setInterval(status,300000); // Run every 5 minutes (300000 ms)
function status() {
++counter; // Add 1 to counter
if( counter % 5 ) { // True when not a multiple of 5
alert( "Write Ajax-type code here to keep session alive" );
} else { // 'counter' is a multiple of 5
alert( "Here, show confirmation user wants to stay logged in" );
// If no click to confirm happens, sign out the user
}
}
Tip: because our status() function doesn't take any argument, we can just pass its unquoted name to setInterval() on line 2, which saves 4 bytes and makes your script more readable!
Sister methods:
• setTimeout() starts a single countdown timer, cancellable by clearTimeout() before it expires.
• setInterval() launches a series of recurring timers that run until stopped with clearInterval().