Speaking words in an array

Viewed 316

I have an array of words that I would like to say aloud using the Web Speech API.

The words are one, two, three, four, five.

When speaking the words on Chrome, the script below says the first word and then repeats the last word until the iteration is complete, the speech output is:

one, five, five, five, five.

On Firefox, the script says the first word and then stops, the speech output is:

one.

I've logged things to the console in an attempt to figure out what's going on, this is the result from both browsers.

word: one
message: one 
word: two 
message: two 
word: three 
message: three 
word: four 
message: four 
word: five 
message: five

The output to the console is desired speech output so I am unsure what exactly is happening.

var msg = new SpeechSynthesisUtterance();
var words = ['one', 'two', 'three', 'four', 'five'];

for (var i = 0; i < words.length; i++) {
    msg.text = words[i];

    console.log('word: ' + words[i]);
    console.log('message: ' + msg.text);

    window.speechSynthesis.speak(msg);
}

What could be causing this and how can I go about correcting it?

1 Answers
Related