A simple example of count down timer using the
setTimeout is shown in the jsfiddle below. Run the code from the Result tab.
All this above function does is to update the
span content every 1000 milliseconds. As you can see here the
setTimeout is applied to the
window object. We also use a global variable to determine when to stop the timer. When the
countDown() function is called timeout of 1000 milliseconds is set for countDown() function. If you repeatedly click on the start button, timeout of 1000 milliseconds is added at different times.
Now that we understand the simplest timer function, we will go ahead and make improvements to it.