Timer notes > One-shot timer with Clear

Turn the background light green and reset the window status bar five seconds after the button is clicked.


The JavaScript and HTML, with a second button to clear the timer before it goes off.

var timer;

function changeScreen( )
{
    document.bgColor = "#ccffcc";
    window.status = "";
}

function setFutureEvent( )
{
    timer = window.setTimeout( "changeScreen()", 5000 );
    window.status = "Timer has been set";
}

function clearTimerAndStatus( )
{
    window.clearTimeout( timer );
    window.status = "";
    timer = null;
}

<form name="myForm" action="#">
<input type="button" value="Set 5-second delay" 
    onclick="setFutureEvent( )" />
<input type="button" value="Clear Timer"
    onclick="clearTimerAndStatus( )" />
</form>