Timer notes > Interval timer using setTimeout()

Start a counter that counts down from ten to zero at one-second intervals in the window status bar when you click the button.


The JavaScript:

var timer;
var counter = 10;

function startCounting( )
{
    timer = window.setTimeout( "countDown()", 1000 );
    window.status = counter;    // show the initial value
}

function countDown( )
{
    counter = counter - 1;
    window.status = counter;
    if (counter == 0)
    {
         window.clearTimeout( timer );
         timer = null;
    }
    else
    {
        timer = window.setTimeout( "countDown()", 1000);
    }
}

<form name="myForm" action="#">
<input type="button" value="Start counting" 
    onclick="startCounting( )" />
</form>