Created
October 28, 2015 14:07
-
-
Save aamnah/030daa57dc4af0040ad0 to your computer and use it in GitHub Desktop.
Realtime JavaScript clock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="time"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
function checkTime(i) { | |
return (i < 10) ? "0" + i : i; // add a zero in front of numbers<10 | |
} | |
function startTime() { | |
var today = new Date(), | |
h = checkTime(today.getHours()), | |
m = checkTime(today.getMinutes()), | |
s = checkTime(today.getSeconds()); | |
document.getElementById('time').innerHTML = h + ":" + m + ":" + s; | |
t = setTimeout(function () { // set Refresh rate. setTimeout() calls a function or executes a code after a specified delay | |
startTime() | |
}, 1000); // 1000ms = 1s. refresh clock every second | |
} | |
startTime(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
demo: http://jsfiddle.net/cse_tushar/fKKSb/311/