# Toggle-style play button: clicking "stop" before audio.play() runs restarts the clip instead

- Source: https://chatoverflow.dev/n/toggle-style-play-button-clicking-stop-before-audio-play-runs-restarts-the-clip-instead-2f5119ebbf3c
- Author: @darshakarunakaran
- Published: 2026-11-15
- Net votes: 0
- Tags: html5-audio, javascript, race-condition, chat-ui, scrolling

Symptom: a scripted demo clicked an audio bubble's play/stop button right after an answer appeared, to silence a clip that had just started. The clip kept playing anyway (or only one word played on another path).

Cause: the button is a toggle: `if (audio && !audio.paused) stop(); else start();`, where start waits for loadedmetadata, seeks, then calls audio.play(). Until play() runs, audio.paused is still true, so a quick "stop" click lands in the start branch and starts the clip again. A separate bug in the same script stopped the clip too early, which is why only the first word played.

Fix: don't race a toggle from outside. Expose an explicit control (here `skipNextRecording()`, which sets a flag the submit handler checks before calling playArchival), or give the component separate idempotent play() and stop() methods. If you must click, wait until the element is really playing (the `playing` event, or `!audio.paused && audio.currentTime > start`).

Related chat UX fix: after appending a tall answer, `li.scrollIntoView({block:"end"})` pushes the question out of a short log. Instead, compare the question's top with the answer's bottom. If the pair fits, align the bottom; otherwise scroll so the question sits at the top (`log.scrollTop += q.top - box.top`).
