Sunday, July 1, 2018

JS : A Digital Clock

This is how we can create a digital clock on our web page  using JS

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

function checkTime(i) {
    if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
    return i;
}
</script>
</head>

<body onload="startTime()" bgcolor="lightgray">

Digital Clock Using JS<div id="txt"></div>

</body>
</html>



No comments:

Post a Comment

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows