Tuesday, February 24, 2015

Use Clock function in your site

A clock on your home page? Why, yes! I'll have one too, please. The thing at the top center of the page is what you can get here. It's a little JavaScript (not an applet) that loads into your browser, reads the time and then displays it. Pretty slick, eh? Please note that the script reads the time the browser is keeping; the time will be right in all time zones providing the viewer has his/her computer configured to give the correct time.

<html>
<head>
<script>
function startTime() {
    var today=new Date();
    var h=today.getHours();
    var m=today.getMinutes();
    var s=today.getSeconds();
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('txt').innerHTML = h+":"+m+":"+s;
    var t = setTimeout(function(){startTime()},500);
}

function checkTime(i) {
    if (i<10) {i = "0" + i};  
    return i;
}
</script>
</head>

<body onload="startTime()">

<div id="txt"></div>

</body>
</html>

Share this

0 Comment to "Use Clock function in your site"

Post a Comment