I have an array that creates a conversation between two people where it 'flips' between the browser speaking and the browser listening.
Short version, how do I make saySomething
queue
for(var i=0; i < speaks.length; i++) {
saySomething(speaks[i]); // saySomething should wait for the previous loop to complete
}
Long Version
var speak = {}; var speaks = [];
speak.utter = "What do they call a quarter pounder with cheese in Paris?"
speak.speaker = true;
speaks.push(speak);
var speak = {};
speak.utter = "They don't call it a quarter pounder with cheese?"
speak.speaker = false;
speak.time = 3; // listen for 3 seconds
speaks.push(speak);
var speak = {};
speak.utter = "They got the metric system. They call it a Royale with cheese"
speak.speaker = true;
speaks.push(speak);
for(var i=0; i < speaks.length; i++) { //foreach better?
var speak = speaks[i];
if(speak.speaker)
speakThePhrase(speak); // uses SpeechSynthesisUtterance
else
listenAndCompare(speak); // uses webkitSpeechRecognition
// don't attempt to speak while listening is happening and vice-versa
}
doSomethingElse(); don't process this till old the speaking/listening is done
Originally I had a recursive loop shifting the array and doing much the same as the above but I think this way might be easier to control the queue. So either using jQuery deferred or Javascript Q how would I go about this?