Pages

Friday, May 11, 2012

Javascript Clock

Javascript
<script type="text/javascript">
function my_clock() {
input = document.getElementById('input');
d = new Date();
h = d.getHours();
m = d.getMinutes();
s = d.getSeconds();
if(h<10){
h = "0"+h;
}
if(m<10){
m = "0"+m;
}
if(s<10){
s = "0"+s;
}
time = (h + ":" + m + ":" + s)
input.value = time;
setTimeout("my_clock()", 1000);
}
</script>
HTML
Function called in the onload event of body, so whenever page loaded clock will be running.
<body onload="my_clock()">
<input type="text" disabled="disabled" id="input" />
</body>

No comments:

Post a Comment