I play fast music, and most metronomes I tried stop at 240 or 300 BPM. So I built one that goes to 1000: Unlimited Metronome . It is free, needs no account and works offline. This post is about the parts that turned out to be harder than "play a click every N milliseconds".
Why setInterval is not a metronome
The obvious version is a timer that plays a sound on every tick. It sounds fine at 80 BPM and falls apart as soon as anything else happens on the page: timers drift, the main thread is busy rendering, and browsers throttle timers in background tabs to once a second or less. At 260 BPM a beat is 230 ms long, so even a 20 ms wobble is audible.
The fix is the pattern from Chris Wilson's article "A Tale of Two Clocks": keep two clocks with different jobs.
A coarse JavaScript timer wakes up every 25 ms.…