jquery - Exiting a for loop with button in Javascript -


hey i've been having difficult time trying code recognize when user presses stop button exit loop. know it's threading issue i've been trying work around settimeout/setinterval.

in code have button onclick="stop()" function stop(){cancel = true;}. in code have .each loop iterates through rows amount of times (depends on amount of check boxes checked). each iteration calls row(this); which, aside checks , getters, contains for-loop iterates total length of current row. like:

for (i = 0; < row.length; i++) {           ajaxcallforrowsingle(row[i]);      if (stop == true) {         return false; } }   

i've tried settimeout(ajaxcallforrowsingle(row[i]),0); "syntax error label not found" assume ajax call. there work around this? button press gets queued , executes after loop.

okay, here's need do...

instead of using 'for' loop, make function , call recursively using, 'settimeout' or 'setinterval'

var irowstoprocess = row.length; var irowindex = 0; var processrows = function(){     if (!stop){         ajaxcallforrowsingle(row[irowindex]);         irowindex++;         if (irowindex < irowstoprocess){             settimeout(processrows, 1);         };     }; };  settimeout(processrows, 1); 

this code call 'processrows' recursively until rows processed, or until 'stop' false. beauty each, "settimeout" call processed asyncronously, means, allows other js code update before called again. releases 1 thread js code syncronously waiting for.


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -