Metronomes in JavaScript
As an audio developer, making a timer metronome is the wrong approach. Still, many metronome apps with this design flaw exists and... they perform bad.
(that's why prescheduled audio events works best...)<p>Timers in general will jitter.<p>Audio processing usually involves a callback to process an audio block (array of audio samples... floats).<p>To simplify things, this audio block runs on realtime thread and copies the block to desired output. you're guaranteed it'll be continuous between each call.
(you can cause overflow/stutter if you hold it for too long).<p>With setTimer approaches each callback WILL have offset.
But if you need to synchronize things or your metronome needs to run with other audio, it WILL be jerky.
That's why a proper metronome would also be 'sample accurate'.<p>WebAudio / AudioContext was made exactly to solve that!<p>similar to common audio processing standard, you end up getting a callback and usually need to use WebAsm/C++ under the hood to keep the golden DSP rule - never allocate on block processing callback.<p>btw, on Firefox 69 (macOS) only the pre-scheduled audio made a sound :)