summaryrefslogtreecommitdiff
path: root/tempo.js
diff options
context:
space:
mode:
Diffstat (limited to 'tempo.js')
-rw-r--r--tempo.js14
1 files changed, 10 insertions, 4 deletions
diff --git a/tempo.js b/tempo.js
index 682cb75..7b471ae 100644
--- a/tempo.js
+++ b/tempo.js
@@ -29,6 +29,14 @@ function updateTime() {
dd + ' ' + MONTHS[dm] + ' ' + dy;
}
+// recursively calling setTimeout instead of using setInterval allows us to
+// correct for any drift caused by imprecise delays.
+function updateTimeRecursive() {
+ updateTime();
+ // +1ms to avoid updating too early
+ setTimeout(updateTimeRecursive, 1001 - new Date().getTime() % 1000);
+}
+
function startTempo() {
timefmt = localStorage['timefmt'];
@@ -40,11 +48,9 @@ function startTempo() {
if ((val = localStorage['bg']))
document.body.style.backgroundColor = val;
- updateTime();
- setTimeout(function() { updateTime(); setInterval(updateTime, 1000) },
- 1000 - (new Date()).getTime() % 1000);
+ updateTimeRecursive();
}
window.onload = startTempo;
-document.addEventListener('contextmenu', function(e) { e.preventDefault() });
+document.addEventListener('contextmenu', e => e.preventDefault());