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.

Idle0.5 ms40 ms bursts40.5 ms120 ms bursts120.5 ms
95th-percentile lateness by page load — chart drawn from the table below.
Page loadAverage latenessMedian95th percentileWorst
Idle0.4 ms0.4 ms0.5 ms0.9 ms
40 ms bursts13.7 ms0.3 ms40.5 ms44.1 ms
120 ms bursts70.5 ms70.5 ms120.5 ms120.5 ms
Idle · median0.4 msIdle · 95th pct0.5 ms40 ms bursts · median0.3 ms40 ms bursts · 95th pct40.5 ms120 ms bursts · median70.5 ms120 ms bursts · 95th pct120.5 ms
Lateness of scheduled clicks from the table above (grey: median, blue: 95th percentile). In our two simulated loads, the slowest 5% of clicks were late by roughly the length of one burst of work.

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:

  1. A timer wakes up often (our metronome uses every 25 ms) and looks a short distance ahead (we use 150 ms).
  2. Any beat that falls inside that window is handed to the audio system with its exact start time.
  3. 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 loadClicks handed over after their planned timeWorstBeat light lag, medianBeat light lag, 95th percentile
Idle1 of 61 (the first)11.0 ms1.5 ms2.8 ms
40 ms bursts0 of 61none2.0 ms32.3 ms
120 ms bursts0 of 61none51.5 ms121.7 ms
200 ms bursts16 of 61192.3 ms206.5 ms351.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).

You can download what we recorded. Both files are plain CSV with a header row:

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

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

  1. Chris Wilson, A Tale of Two Clocks: Scheduling Web Audio with Precision (web.dev)
  2. MDN: BaseAudioContext.currentTime
  3. MDN: setInterval() and delay restrictions
  4. 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.

More guides

Sound & timeWhite, pink or brown noise? We measured what each one actually containsWe analysed the noise our generator produces, octave by octave. The difference is where the energy sits, and it explains why brown noise sounds 'softer'.Read guide →Sound & timeWhy your international meeting moved by an hour: the daylight saving gaps of 2026The US, Europe and Australia change clocks on different dates. We computed every 2026 change and the weeks when the usual time difference is wrong.Read guide →Sound & timeWhen is a leap-day baby a year older? Age calculation edge cases, testedWe ran nine tricky date pairs through our age calculator and a common programming shortcut. One shortcut turns January 31 plus one month into March 3 in a non-leap year.Read guide →