articles

Home / DeveloperSection / Articles / Creating Simple Timer in Java Script

Creating Simple Timer in Java Script

Anonymous User8756 15-Mar-2011

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

 


Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By