Online Metronome › Guides
Can a browser metronome keep time? setInterval against the audio clock
Open the tool: Online Metronome
A metronome has one job: clicks at exactly even intervals. On a dedicated device that is easy. In a web page it is not, because JavaScript timers share a single thread with everything else the page does: drawing, scrolling, handling input, running other scripts. We measured how much that matters, and how a properly built web metronome avoids it.
Short answer: a click fired directly by setInterval was within 1 ms on an idle page but as late as the longest simulated burst of page work (120 ms). Clicks scheduled ahead on the Web Audio clock landed exactly 24,000 samples apart at 120 BPM in an offline render, because the audio system places them by design rather than by the busy JavaScript thread. Our metronome uses the audio-clock method.
Test 1: timer-driven clicks
In Chromium 153 we started a setInterval with a 500 ms period (120 BPM) and recorded, for 60 beats, how late each callback ran compared with the ideal time. We repeated the test three times: on an idle page, with a 40 ms burst of work every 150 ms, and with a 120 ms burst every 150 ms. The bursts imitate a page that is busy with animations, a heavy framework or ads.
| Page load | Average lateness | Median | 95th percentile | Worst |
|---|---|---|---|---|
| Idle | 0.4 ms | 0.4 ms | 0.5 ms | 0.9 ms |
| 40 ms bursts | 13.7 ms | 0.3 ms | 40.5 ms | 44.1 ms |
| 120 ms bursts | 70.5 ms | 70.5 ms | 120.5 ms | 120.5 ms |
On an idle page the timer was excellent. Once other work competed for the thread, any beat that fell during a burst had to wait until the burst finished. With 40 ms bursts most beats were still on time, but one in several was 40 ms late, and an irregular 40 ms slip in a steady pulse is often noticeable. With heavier load the clicks were late on average by 70 ms and the rhythm fell apart.
Test 2: clicks scheduled on the audio clock
The Web Audio API has its own clock, AudioContext.currentTime, driven by the sound hardware rather than the JavaScript thread. A sound can be told to start at an exact future time on that clock, and the audio system plays it at that sample regardless of what the page is doing. We rendered eight clicks at 120 BPM with an offline audio context using the same click sound as our metronome, then located the start of each click in the output.
The clicks began at samples 2,454, 26,454, 50,454 and so on. Every interval was exactly 24,000 samples, which at 48,000 samples per second is exactly 500.000 ms. There is no jitter to report.
How a good web metronome combines the two
JavaScript is still needed to decide what to play next, so the standard technique, described by Chris Wilson in "A Tale of Two Clocks", uses both clocks:
- A timer wakes up often (our metronome uses every 25 ms) and looks a short distance ahead (we use 150 ms).
- Any beat that falls inside that window is handed to the audio system with its exact start time.
- If the timer itself runs late, nothing is lost as long as it runs before the look-ahead window is used up, because the beats were already scheduled.
With a 150 ms look-ahead, a page can stall for roughly 125 ms (the 150 ms look-ahead minus the 25 ms timer interval) without a single click moving. The visual beat indicator still uses an ordinary timer, so on a very busy device the flashing dot may lag slightly behind the sound; the sound is the part to trust.
Our metronome, tested from its screen
The tests above use bare timers and an offline render. To see how the finished page behaves, we opened our metronome in Chromium 157 and used the browser testing framework Playwright to press +5 four times (the display read 120 BPM), press Start and let it play 61 clicks in 4/4. The script recorded each click as the page handed it to the audio system, with the time it was planned for and the audio clock at that moment, and noted when each beat light came on. We ran this on an idle page and again with the same kind of bursts of busy work every 150 ms as in test 1, at 40, 120 and 200 ms.
| Page load | Clicks handed over after their planned time | Worst | Beat light lag, median | Beat light lag, 95th percentile |
|---|---|---|---|---|
| Idle | 1 of 61 (the first) | 11.0 ms | 1.5 ms | 2.8 ms |
| 40 ms bursts | 0 of 61 | none | 2.0 ms | 32.3 ms |
| 120 ms bursts | 0 of 61 | none | 51.5 ms | 121.7 ms |
| 200 ms bursts | 16 of 61 | 192.3 ms | 206.5 ms | 351.1 ms |
The beat light lag is the time on the audio clock when the light came on, minus the time the click was planned for; the 95th percentile is the 57th of the 60 values in order (nearest-rank method).
- The planned grid never moved: in all four runs the planned times were exactly 500 ms apart, so the planned tempo did not drift in any run. What load can change is whether the page hands a click over before its time arrives. A click handed over late is played straight away, so it sounds late.
- The look-ahead held up to 120 ms: with 40 and 120 ms bursts every click was handed over at least 9 ms before it was due. With 200 ms bursts, longer than the roughly 125 ms the look-ahead can cover, 16 clicks were late by up to 192 ms. We tested a desktop browser only; late clicks appeared when this page itself was blocked for longer than the look-ahead covers.
- The very first click: on the idle run the first click was handed over 11 ms after its planned time. Only the gap between the first and second click is affected; every later click was on time.
- Accents: in every run, clicks 1, 5, 9 and so on used the higher 1,600 Hz tone and all others 1,000 Hz, so the first beat of each bar stayed marked.
- The light is the weak part: the beat light runs on an ordinary timer, so under 120 ms bursts it came on up to 123 ms after the sound while the clicks themselves stayed on time. Trust your ears over the light on a busy device.
- Tap tempo: the script pressed "Tap tempo" eight times with a wait of 400, 500, 600 or 750 ms between presses, three times each, and the page recorded the actual press times. Each press also took some time to perform, so the real gaps were about 16 to 21 ms longer than the waits. In all 12 runs the tempo shown was exactly 60,000 divided by the average real gap, rounded to a whole BPM; for example, gaps averaging 519 ms gave 116 BPM. A pause of more than two seconds starts a new measurement, and each run gave its own result rather than an average with the previous one.
You can download what we recorded. Both files are plain CSV with a header row:
- Every click in the four runs: columns page_load_burst_ms, beat, scheduled_time_s, audio_clock_when_scheduled_s, scheduled_late_ms, tone_hz and light_lag_ms.
- The tap tempo runs: columns script_wait_between_taps_ms, repeat, taps, measured_mean_interval_ms, bpm_from_measured_interval, bpm_shown and tempo_name_shown.
Background tabs
Browsers deliberately slow timers in hidden tabs to save battery; Chrome documents limits such as running timers at most once per second in background pages, with stricter limits after a page has been hidden for several minutes. A metronome built only on timers will stutter as soon as you switch tabs. Audio-clock scheduling is more robust, because pages that are playing audio are typically exempt from the strictest throttling, but for practice it is still best to keep the metronome in a visible tab or window.
How to check any web metronome
- Listen while scrolling. Open the metronome, start it, then scroll quickly or open another heavy page. If clicks stumble, it is timer-driven.
- Record it. Record a minute of clicks with your phone and look at the waveform in a free audio editor. The gaps between peaks should be identical.
- Check the tempo over time. At 100 BPM, 60 seconds should contain exactly 100 clicks. Count the clicks over two minutes against a stopwatch.
The same lesson for countdown timers
Timers face the same problem in a milder form. A countdown that subtracts one second every time a one-second timer fires will drift, because each callback can be late and the errors add up. Our timer stores the end time and recalculates the remaining time from the system clock several times a second, so the remaining time is correct whenever the display updates. The alarm sound is triggered by the same timer, though, so in a background tab it can sound late; keep the tab visible for alarms that matter.
Common questions
Does Bluetooth add delay?
Yes. Wireless headphones and speakers typically add a constant delay of tens to hundreds of milliseconds. The clicks stay evenly spaced, so it does not matter for practising alone, but it can when you play along with other sound sources. Wired headphones avoid it.
Why does the first click sometimes come late?
Browsers do not allow audio to start until you interact with the page, and the audio system needs a moment to start up. A good metronome schedules the first beat a little in the future so the start is clean.
Is a phone app more accurate?
A well-built app and a well-built web metronome both rely on the device's audio clock and can be equally steady. The weak point is not the platform but whether the clicks are scheduled on the audio clock or fired by a general-purpose timer.
References
- Chris Wilson, A Tale of Two Clocks: Scheduling Web Audio with Precision (web.dev)
- MDN: BaseAudioContext.currentTime
- MDN: setInterval() and delay restrictions
- Chrome for Developers: Heavy throttling of chained JS timers
Figures without a source link come from the steps shown on this page. Companion tool: Online Metronome. Spotted an error? Write to help@glowhalo.org. Chrome, Chromium and Bluetooth are trademarks of their owners; GlowHalo is not affiliated with them.