javascript - JS Loop - how do I loop through X items then wait for response to finish the loop -


i want loop through x items want loop through 2 of items wait response function when done start loop on. so, 2 loops run @ given time until items haven finished. best efficient way accomplish this?

the news single-threaded nature of javascript works advantage here. bad news if work requires form of asynchronous input, you'll have a) write thread-blocking/locking code, or b) creative recursion , settimeout.

the simpler solution (a) goes this. let's assume need prompt user every 2 loops continue operation.

for( var i=0, n=0; < 10000; i++, n++ ){   if ( n == 2 ) {     /* example of thread blocking operation          "confirm" function blocks thread,          loop stop executing until user          clicks "ok"      */      if ( !confirm("keep looping?") ){       break;     };      // reset n fires again in 2 iterations     n = 0;   } } 

of course, if work doesn't require asynchronous input user or ajax call, better.

fortunately, more graceful solution around corner. latest javascript spec (es6) uses called generators accomplish you're trying (you can think of them async-await). unfortunately, support generators varies , considered more advanced topic, mileage may vary.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -