Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Keeping a browser timer accurate in background tabs (what I learned building ClockAura)

I run ClockAura , a free site of online timers: countdowns, a Pomodoro timer, a stopwatch, interval timers for the gym. The first versions had the bug I suspect every browser timer has at some point: start a countdown, switch to another tab, and it rings late. Sometimes minutes late. The bug wasn't in one line of code. It came from an assumption: that the browser keeps running your code at the…

The issue with browser timers in background tabs is a common problem that many timer applications face. The first version of ClockAura, a free online timers site, had this bug: when a countdown was started and the user switched to another tab, the timer would ring late, sometimes even minutes late. The root cause of the problem was the assumption that the browser keeps running the timer code at the same pace regardless of whether the user is looking at the tab or not. This is not true.

In the naive implementation of a countdown timer, the code used setInterval(fn, 1000) to set an interval of 1000 milliseconds (1 second) between calls. However, this does not guarantee that the callback function will be executed exactly every 1000 ms. The setInterval function only promises at least 1000 ms between calls. This means that over time, due to the accumulation of small delays, drift can occur, leading to the timer ringing late, even in a foreground tab.

Another issue was related to background tabs. Browsers like Chrome limit timers in hidden tabs to roughly one wake-up per second. Furthermore, after a page has been hidden for a few minutes, the browser can batch chained timers more aggressively, sometimes even down to once per minute. This means that a countdown timer would be at the mercy of the browser's wake-up intervals, causing it to run late.

To address these issues, the author implemented two main rules for accurate browser timers:

1. Store the end time, not the remaining time. When the timer starts, compute the exact moment it should end. From that point on, derive the remaining time based on the current time and the target end time. This approach helps avoid drift caused by the browser's wake-up intervals. The code example shows how to store the target end time using performance.now() and update the remaining time by calculating the difference between the target end time and the current time.

2. Don't rely solely on requestAnimationFrame to finish the job. While requestAnimationFrame can be used for smooth display updates, it pauses entirely in hidden tabs. This means that a timer using only requestAnimationFrame would finish the moment the user switches back to the tab. To fix this, the author added a second mechanism using setTimeout, which aims directly at the end time. This approach ensures that the timer finishes at the correct time even if the browser pauses the requestAnimationFrame loop.

The author also shared some additional tips and tricks to improve the reliability of browser timers, such as using a single long timeout instead of chained timers and adding a small padding (e.g., 15 ms) to the timeout to account for any slight inaccuracies.

By following these rules and best practices, developers can create more reliable and accurate browser timers that work consistently across different browsers and maintain the correct timing even when the timer is in a background tab.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

BirthStar: One Birthday, Four Traditions, One Star Chart

This is a submission for the Hacktoberfest Open-Source AI Challenge: Week 2 — Side Hustle. What I built BirthStar — a single-file web app that turns one birthday into a star chart and a detailed…

  • BirthStar generates star chart from one's birthday
  • Combines Western, Chinese, and moon phase traditions
  • Free, open-source, and privacy-focused application

A 180-page reference site for the price of a domain (Astro + Cloudflare Workers)

I built steelweight-calculator.com , a reference site for steel section weights: one page per beam, channel and angle, weight charts, pipe schedules, rebar and sheet gauges, plus calculators.

  • 180-page reference site covers steel section weights
  • Generated from data using Astro and Python scripts
  • Hosted on Cloudflare Workers with no additional code

More from Saturday, 17 October 2026 →