Home > DeveloperSection > Beginner > Creating Simple Timer in Java Script

Creating Simple Timer in Java Script


JavaScript JavaScript 
Ratings:
0 Comment(s)
 5313  View(s)
Rate this:

Creating Simple Timer in Java Script

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

 


Don't want to miss updates? Please click the below button!

Follow MindStick