Created
October 16, 2012 21:29
-
-
Save micah1701/3902167 to your computer and use it in GitHub Desktop.
jQuery Digital Clock that attempts to extend accuracy by removing lag time caused by slow processors
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
<script> | |
/** | |
* Show the time and update it every second | |
* | |
* clock_id (string) DOM id value of element to update | |
* offset (int) Optional value to modify local time by, usefull if synchronizing to another clock | |
*/ | |
function clock(clock_id, offset) | |
{ | |
if(offset == null || offset == ""){ offset = 0; } | |
var d = new Date(); | |
d.setMilliseconds( d.getMilliseconds() + offset * 1000 ); | |
var milliseconds = d.getMilliseconds(); | |
var timer_interval = 1000 - milliseconds; // run this function again, slightly less than 1 second from now | |
$("#"+clock_id).html( ("0"+d.getHours()).slice(-2) +":"+ ("0"+d.getMinutes()).slice(-2) +":"+ ("0"+d.getSeconds()).slice(-2) ); | |
setTimeout(function(){ clock(clock_id,offset); }, timer_interval ); | |
} | |
</script> | |
<body onLoad="clock('local_time')"> | |
<h1 id="local_time"></h1> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment