From dfe96894b33e9458dc054c9b265fb8f0557ae6cb Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Sun, 13 Apr 2025 00:22:11 -0700 Subject: correct for drift --- tempo.js | 14 ++++++++++---- 1 file 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()); -- cgit v1.2.3-70-g09d2