In this demonstration we are going to learn how to create a simple timer control in java script. For creating a timer control we need to create a Date () object. Then we can retrieve all information such as hours, minutes, seconds and milliseconds. Then we can use setTime(“dispTime()”,1) method to call it recursively.

Following function creates a timer function
function dispTime() {
            var date = new Date();
            document.getElementById("dd1").innerHTML = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + ":" + date.getMilliseconds();
            setTimeout("dispTime()",1);
}

Following html code design a simple UI in html.

Code:
<body onload="dispTime()">
    <div id="dd1">
    </div>
</body>
Output of the following code snippet is as follows

Creating Simple Timer in Java Script

Creating Simple Timer in Java Script

 

  Modified On Nov-24-2017 11:43:35 PM

Leave Comment