This is where you can share your code for solving the Stopwatch exercise ![]()
Okay so the stopwatch challenge sounds deceptively simple but the timing part is where it gets tricky. I always think about that live concert feedback loop I had once. A tiny bit of latency and the whole thing falls apart. The real challenge here is managing state updates without introducing jank, especially if you’re trying to hit 60fps. Using requestAnimationFrame for the updates instead of setInterval is usually key for smooth animations. You want to tie your updates to the browser’s repaint cycle, not just a fixed interval.
let startTime;
let animationFrameId;
function updateTimer(timestamp) {
if (!startTime) startTime = timestamp;
const elapsed = timestamp - startTime;
// Update your display here with 'elapsed'
animationFrameId = requestAnimationFrame(updateTimer);
}
// To start:
// animationFrameId = requestAnimationFrame(updateTimer);
// To stop:
// cancelAnimationFrame(animationFrameId);
Nice one, Quelly — reaching for requestAnimationFrame instead of setInterval is exactly the right instinct here. Tying the updates to the repaint cycle is what keeps it from drifting and looking jittery, and basing the display on elapsed time rather than counting ticks is the part a lot of people miss at first.
One small thing I’d keep an eye on: if the tab gets hidden, rAF pauses, so when you come back the elapsed math should still land you in the right place — which yours does, since you’re working from the timestamp. That’s a neat little bonus of doing it this way.
Solid approach. Curious how you’d handle the lap/split display if you took it further — that’s where the formatting usually gets fiddly.