Digital Clock Using Javascript
This tutorial focuses on the setTimeout function for javascript, using which we can set a interval to a span tag and make it like a working digital clock.
You can choose any tag in the HTML and force to refresh it on a set interval on its own, without making the page load or heavy using ajax calls to the server, below is the complete HTML code for a Digital Clock using javascript.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<title>Digital Clock | Javascript</title>
<script language="JavaScript" type="text/javascript">
function showtime() {
now=new Date();
hour=now.getHours();
min=now.getMinutes();
sec=now.getSeconds();
if (min<=9) { min="0"+min; }
if (sec<=9) { sec="0"+sec; }
if (hour>12) { hour=hour-12; add="pm"; }
else { hour=hour; add="am"; }
if (hour==12) { add="pm"; }
time = ((hour<=9) ? "0"+hour : hour) + ":" + min + ":" + sec + " " + add;
if (document.getElementById) { document.getElementById('theTime').innerHTML = time; }
else if (document.layers) {
document.layers.theTime.document.write(time);
document.layers.theTime.document.close(); }
setTimeout("showtime()", 1000);
}
window.onload = showtime;
</script>
</head>
<body>
<span id="theTime" style="color:black; z-index:2; position:absolute; text-align:centre; font-family: arial; font-size: 60pt"></span>
</body>
</html>
Happy Coding !!
Comments
Post a Comment