// Execute function after 1 second:
setTimeout( "doSomething()", 1000 );
But this kind of anonymous timer cannot be stopped (short of reloading the entire page).var tmr = setTimeout( "doSomething()", 1000 );
var tmr1 = setTimeout( "alert('Reached first timer!')", 1000 );
var tmr2 = setTimeout( "alert('Second timer reached!')", 1500 );
clearTimeout( tmr1 ); // Click over there to test this code ->
If you ran the code above, you noticed that the first timer was cleared before it got a chance to reach its time limit; but the second timeout (whose variable wasn't passed) worked just fine. Note: JavaScript will not throw an error message if you try to cancel a timer already elapsed.
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().