I tried the following on Google Chrome. It'd print out 0 voices, and again print out 0 voices on my computer, but if I reload the page to start fresh, and change the last line to setTimeout(foo, 1000), then the first line would print out 0 and second line, 87.
The number could even be changed to 66 or 33 and it worked, but I am not sure whether it depends on the computer speed and the network speed (for fetching the voice data. (could it be internal network traffic, as I didn't see any traffic in the debugger's network tab)).
Is there a more reliable way to get a ready event similar to a DOMReady event or some way to invoke a callback, so that it is more reliable? (instead of using a setTimeout to delay for an unknown duration and hoping for the best).
function foo() {
msg = new SpeechSynthesisUtterance(),
voices = window.speechSynthesis.getVoices();
console.log("How many voices", voices.length);
msg.text = "Hello World";
speechSynthesis.speak(msg);
}
foo();
setTimeout(foo, 0); // just try again in the next event cycle