summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2025-04-13 00:22:11 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2025-04-13 00:22:11 -0700
commitdfe96894b33e9458dc054c9b265fb8f0557ae6cb (patch)
tree3c14ae4dff485930c47af861b48efb98c7ff3a44
parentace00c386fd5cfaf3bf9cd28e7f8022c31c0eb63 (diff)
downloadtempo-dfe96894b33e9458dc054c9b265fb8f0557ae6cb.tar.gz
tempo-dfe96894b33e9458dc054c9b265fb8f0557ae6cb.tar.xz
correct for drift
-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());