wikly.app

The alarm is booked before it is needed

Here is a bug that every browser-based timer has to survive, and that a surprising number of them do not. You start a twenty-five minute session, switch to another tab to do the actual work, and come back to find that nothing ever rang.

The cause is not mysterious. A tab nobody is looking at is a tab the browser would rather not spend a processor on, so its JavaScript timers get slowed down. In Chrome a background tab's timers fire at most once a second. After five minutes hidden they drop to about once a minute. Eventually the tab can be frozen outright, at which point nothing fires at all until you come back — and by then you have already come back, which is exactly when an alarm is useless.

So the obvious implementation is the broken one. You cannot set a timer for twenty-five minutes, wait for it to go off, and then play a sound, because the part that waits is the part the browser is allowed to stop.

Another clock

The Web Audio API has its own clock, and it does not share the browser's opinions about background tabs. It runs on the audio thread, in real time, because audio that drifted when you looked away would be an unusable platform for anything.

More usefully, it lets you schedule. Every oscillator and every volume change can be given a time to happen at, and that time can be a long way off. So Wikly does not wait for the session to end. The moment there is a session, the whole alarm — every note of it — is handed to the audio clock with instructions to sound in exactly as many seconds as the plan has left. Then nothing has to be watching. The tab can be hidden, throttled, frozen; the sound is already booked, and it arrives on time.

The alarm itself is two notes a fifth apart, the second a beat behind the first, repeated three times with each round a little louder. It is synthesised rather than played from a file, which is why the app ships no audio at all.

The things that still need the screen

Two small parts cannot be booked in advance, because they are not sound. One is the flag that tells the radio to step down while the bell rings. The other is listening for you to answer it. Both of those do run on an ordinary JavaScript timer, and both of them can therefore be late.

Being a little late costs nothing — except in one case. A frozen tab can deliver that timer long after the sound has come and gone, so if it arrives more than the length of the alarm after the moment it was due, it is thrown away instead of acted on. Ducking the music for a bell that finished four minutes ago would be worse than saying nothing.

Why the dial has to be tapped

One consequence worth knowing. Browsers keep audio shut until a person does something, which is a good rule and not one anybody should want lifted. The audio context is therefore created and woken on the first real gesture — dragging the dial, tapping it, pressing a key on it.

That is why starting a session is a deliberate act rather than something that can happen on a page load. It is also why, if you ever manage to get a session running without touching the dial, the alarm at the end will go off in silence. The gesture is not ceremony. It is the thing that opens the speaker.

And if you reopen the desk to find a session already past its plan, nothing is booked and nothing rings. The clock is counting up. That is the notification.