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...)Timers in general will jitter.Audio processing usually involves a callback to process an audio block (array of audio samples... floats).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).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'.WebAudio / AudioContext was made exactly to solve that!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.btw, on Firefox 69 (macOS) only the pre-scheduled audio made a sound :)