Java Script tutorial 14
Today I am going to show you how to make a timer which execute a function within a fixed
var count = 0;
var pdata = document.getElementById('p1');
pdata.innerHTML = pdata.innerHTML + '<br />Counter no: ' + count;
if(count <= 9)
count = 0;
<input type="button" onclick="set_timer()" value="Start timer" />
Above we created a button with bounded with a function set_timer() at onclick event. When user clicks on the button the function inside script is executes. we created there a counter initial value is 0 and the at first count it increments by one and set innerHTML of paragraph p1 as Counter no: 1, then it sets the timer for 500ms to execute this function again and its work like recursion till the value of count do not exceed 9 at 10th loop the else executes and set the value of Count again to 0.
The setTimeout method takes two arguments first is the function will be execute and second
is the time after that the function will be execute.