setInterval accumulates drift because execution time is included.
✔ Fix: Self-adjusting timer
const start = Date.now();
let count = 0;
function tick() {
count++;
const next = start + count * 1000;
setTimeout(tick, next - Date.now());
}
tick();
Accurate to the millisecond.
