JavaScript > Timer

Create Timer in JavaScript

How to create a timer in JavaScript?


Timer is a kind of event that automatically executes after specified number of mili-seconds. To use timer in JavaScript, we can follow this approach.

   <script>
        var count = 0;
        function TestFunction() {
            count++;
            document.getElementById("lblText").innerHTML = count;
            t = setTimeout("TestFunction()", 1000);
        }
    </script>

    <label id="lblText">0</label>
    <input type="button" id="btnTest" onclick="TestFunction()" value="Click me" />

Above function executes after every 1 second and set the label value after incrementing. As the function is being called in setTimeout method, it will keep executing indefinite number of times.

OUTPUT

To know how to stop timer, click here.

 Views: 7909 | Post Order: 188



Write for us






Hosting Recommendations