A browser timer has two jobs. It needs to show the current time, and it needs to know how much time to save. Those jobs diverge as soon as the browser throttles a background tab, the user pauses for a break, or the network drops.
StudyTimely is the browser-based study timer used as the example here. Its personal study tools keep their own records in the browser. The server has a separate role for features like account sync and shared rooms.
StudyTimely study timer, captured from an earlier release.
Use the display tick to redraw
An interval callback is a convenient way to refresh the screen. Browsers may delay that callback, so incrementing a counter by one on every tick can undercount a running session. Store the timer's boundaries and derive elapsed time when you render.
The following JavaScript is a simplified model of that separation.
function elapsedMs({ completedMs, runningSince }, nowMs) {
const activeMs = runningSince == null
? 0
: Math.max(0, nowMs - runningSince);
return completedMs + activeMs;
}
On pause, close the active interval and add it to the accumulated duration. On resume, start a new interval. The app stores the intervals used for saved sessions so reporting can distinguish a 25-minute window from the actual study within it. The example leaves out clock-change checks. StudyTimely asks for review when a device-clock change makes a timing interval uncertain.
Only the two green study intervals contribute to the saved total.
Keep the plan and the record separate
A 25-minute plan describes what the user wants to do. A saved session describes what happened. For example, if someone sets a 25-minute focus timer and saves after 12 minutes, the history should show 12 minutes. Pauses and Pomodoro breaks stay out of the study total.
This also makes button behavior easier to explain. Pause keeps the current session available to resume. Finish session records the elapsed study. A planner entry can exist before any timer starts, with no time added to the record.
Define what offline means
A service worker caches the app shell and supported tools. IndexedDB stores local study records separately. Once the offline cache is ready, the timer and planner can work without a network connection. Sign-in, cloud sync, and live room features still require a connection.
The distinction matters during recovery. Reopening a cached page tests whether the interface is available offline. Saving a session, reloading, and finding it in history tests local persistence. Opening the app on another device tests a separate feature, optional sync.
If you're implementing a timer, try this sequence before testing a full session. Start, switch tabs, return, pause, wait, resume, and save. Compare the saved duration with the two running intervals. Then repeat the same flow offline after the cache is ready. A clock that looks right can still produce a bad record.
How do you handle an active timer when the same app is open in two tabs?
Disclosure: This article was drafted with AI assistance using the project’s implementation and documentation as source material.


Top comments (0)