Learn the simple JavaScript example code to show the clock timer to display the current time on your web page. For displaying the clock timer you must be familiar with setTimeout function of Javascript to call the function after every specified time delays.
JavaScript example code for Clock Timer
<html>
<head>
<title>JavaScript Clock Timer</title>
<script language="javascript">
function getCurrentTime() {
var currentTime = new Date();
var hrs = currentTime.getHours();
var mnts = currentTime.getMinutes();
var secs = currentTime.getSeconds();
mnts = setTwoDigits(mnts);
secs = setTwoDigits(secs);
document.getElementById('clockTimer').innerHTML = hrs + ":" + mnts + ":" + secs;
var timeOut = setTimeout("getCurrentTime()", 1000);
}
function setTwoDigits(digit) {
if (digit < 10) {
digit = "0" + digit;
}
return digit;
}
</script>
</head>
<body onload="getCurrentTime()">
<div id="clockTimer">
</div>
</body>
</html>