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.

        var count = 0;
        function TestFunction() {
            document.getElementById("lblText").innerHTML = count;
            t = setTimeout("TestFunction()", 1000);

    <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.


To know how to stop timer, click here.

 Views: 937 | Post Order: 188

Write for us