jquery - How do I queue events in a Javascript Loop -


i have array creates conversation between 2 people 'flips' between browser speaking , browser listening.

short version, how make saysomething queue

 for(var i=0; < speaks.length; i++) {      saysomething(speaks[i]);    // saysomething should wait previous loop complete  } 

long version

var speak = {}; var speaks = []; speak.utter = "what call quarter pounder cheese in paris?" speak.speaker = true; speaks.push(speak);  var speak = {};  speak.utter = "they don't call quarter pounder cheese?" speak.speaker = false; speak.time = 3;  // listen 3 seconds speaks.push(speak);  var speak = {};  speak.utter = "they got metric system. call royale cheese" speak.speaker = true; speaks.push(speak);  for(var i=0; < speaks.length; i++) {    //foreach better?         var speak = speaks[i];         if(speak.speaker)               speakthephrase(speak);    // uses speechsynthesisutterance         else               listenandcompare(speak);  // uses webkitspeechrecognition         // don't attempt speak while listening happening , vice-versa     }  dosomethingelse();    don't process till old speaking/listening done 

originally had recursive loop shifting array , doing same above think way might easier control queue. either using jquery deferred or javascript q how go this?

you not creating queue of object shadowing same objects , pushing again speaks array, last version of object several times inside array.

if want create async behavior should try callbacks instead of simple for loop.

it better this:

var speak = [     {         utter: "what call quarter pounder cheese in paris?",         speaker: true     },     {         utter: "they don't call quarter pounder cheese?",         speaker: false,         time: 3     },     {         utter: "they got metric system. call royale cheese",         speaker: true     } ]; 

then can use array methods filter , map handle data need.

finally, can use events trigger each speaking when in application happens.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -