I run VoiceStopwatch, a free stopwatch and timer you can control by saying "start", "stop", "lap" and "reset". It runs entirely in the browser on the Web Speech API, with no app and no account.
The demo version of this takes twenty lines. The version that survives real users took far longer. These are the five problems that cost the most time, and what fixed each one.
1. Recognition stops on its own, all the time
continuous = true does not mean "listen forever". Browsers end the session after silence, after a network hiccup, or just because. If you do nothing in onend, voice control quietly dies and the user keeps talking to a page that is no longer listening.
The fix is to restart in onend, but with a guard so you never restart a session you stopped on purpose:
recognition.onend = () => { if (!active()) return; // we stopped it ourselves current = null; if (++failures > 3) return stop('Speech service keeps ending. Check your microphone.'); setTimeout(start, 500); }; The failure counter matters. Without it, a blocked microphone turns into an endless restart loop.
2. Late events from an old session break the new one
After a restart, the old recognition object can still fire onend or onerror. If those handlers touch shared state, they stop the session you just started.
Each attempt now owns its callbacks and checks that it is still the current one:
const active = () => current === recognition && enabled && !paused; Every handler starts with if (!active()) return;. This one line removed a whole class of "voice randomly turns off" reports.
3. Waiting for final results makes "stop" feel slow
By default you act on isFinal results. The browser only finalises after a pause in speech, so a stopwatch stops noticeably late. For a timing tool that is the one thing it cannot do.
Turning on interimResults lets a one-word command act as soon as it is heard:
recognition.interimResults = true; recognition.maxAlternatives = 3; The catch is that the same phrase then arrives again as a final result. I keep a set of result indexes that were already handled as interim, and skip them when the final version arrives. maxAlternatives helps too: "lap" is often heard as "lab" or "app", and the right word is usually in the alternatives.
4. The timer hears itself
The stopwatch can also speak: it reads lap times and counts down aloud with speechSynthesis. With the microphone open, it hears its own voice and obeys it. A spoken "stopped at ten seconds" can trigger "stop" again.
The fix is to pause listening while the page is speaking and resume about 350 ms after speech ends. The status line says "Listening paused while the timer speaks", so the user knows why a command was ignored.
5. Error codes need human words
not-allowed, service-not-allowed, audio-capture, network, aborted. Users see none of these unless you show them, and each one needs a different action. aborted was the surprising one: it usually means another tab on the same site is already listening.
Every error code now maps to one plain sentence that says what happened and what to do next. Buttons always work as a fallback, and the status line says so when voice is off.
What I would tell myself at the start
- Treat speech recognition as a connection that drops, not as a sensor that stays on.
- Give every session its own identity and ignore events from old ones.
- Measure time with
performance.now()and never with the moment a command was finalised. - Support is uneven. Chrome and Edge work best; some browsers have no recognition at all, so the tool must be fully usable without it.
You can try the result at voicestopwatch.com. If you have built on the Web Speech API and solved the restart problem differently, I would like to hear how.