Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mapio/967b6a65b50d39c2ae4f to your computer and use it in GitHub Desktop.
Save mapio/967b6a65b50d39c2ae4f to your computer and use it in GitHub Desktop.
(Sort of) busy wait workaround for SpeechSynthesisUtterance.onend bug
<button onclick="doit()">Greet…</button>
function speak( text, onend ) {
window.speechSynthesis.cancel();
var ssu = new SpeechSynthesisUtterance( text );
window.speechSynthesis.speak( ssu );
function _wait() {
if ( ! window.speechSynthesis.speaking ) {
onend();
return;
}
window.setTimeout( _wait, 200 );
}
_wait();
}
function doit() {
speak( 'Hello, world!', function() { alert( 'done' ); } );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment