I can not comment on the previous answers, since I have not tried them. However, I know that the following strategy works for me. It's a little less elegant, but it does the job. It also does not require breaking the code into pieces, as, for example, some other approaches. In my case, this was not an option, because my code had recursive calls to logic that was looped; those. there was no practical way to simply jump out of the loop, and then you can somehow resume using global vars to maintain the current state, since these global variables can be changed by referencing them in a subsequent recall. Therefore, I needed a simple way that would not give the chance to the code to compromise the integrity of the data state.
Assuming "stop script?" the dialog appears during the execution of the for () loop after several iterations (in my case about 8-10), and messing with the registry is not an option, there was a fix (for me, anyway):
var anarray = []; var array_member = null; var counter = 0; // Could also be initialized to the max desired value you want, if // planning on counting downward. function func_a() { // some code // optionally, set 'counter' to some desired value. ... anarray = { populate array with objects to be processed that would have been processed by a for() } // 'anarry' is going to be reduced in size iteratively. Therefore, if you need // to maintain an orig. copy of it, create one, something like 'anarraycopy'. // If you need only a shallow copy, use 'anarraycopy = anarray.slice(0);' // A deep copy, depending on what kind of objects you have in the array, may be // necessary. The strategy for a deep copy will vary and is not discussed here. // If you need merely to record the array orig. size, set a local or // global var equal to 'anarray.length;', depending on your needs. // - or - // plan to use 'counter' as if it was 'i' in a for(), as in // for(i=0; i < x; i++ {...} ... // Using 50 for example only. Could be 100, etc. Good practice is to pick something // other than 0 due to Javascript engine processing; a 0 value is all but useless // since it takes time for Javascript to do anything. 50 seems to be good value to // use. It could be though that what value to use does depend on how much time it // takes the code in func_c() to execute, so some profiling and knowing what the // most likely deployed user base is going to be using might help. At the same // time, this may make no difference. Not entirely sure myself. Also, // using "'func_b()'" instead of just "func_b()" is critical. I've found that the // callback will not occur unless you have the function in single-quotes. setTimeout('func_b()', 50); // No more code after this. function func_a() is now done. It important not to // put any more code in after this point since setTimeout() does not act like // Thread.sleep() in Java. Processing just continues, and that is the problem // you're trying to get around. } // func_a() function func_b() { if( anarray.length == 0 ) { // possibly do something here, relevant to your purposes return; } // -or- if( counter == x ) // 'x' is some value you want to go to. It'll likely either // be 0 (when counting down) or the max desired value you // have for x if counting upward. { // possibly do something here, relevant to your purposes return; } array_member = anarray[0]; anarray.splice(0,1); // Reduces 'anarray' by one member, the one at anarray[0]. // The one that was at anarray[1] is now at // anarray[0] so will be used at the next iteration of func_b(). func_c(); setTimeout('func_b()', 50); } // func_b() function func_c() { counter++; // If not using 'anarray'. Possibly you would use // 'counter--' if you set 'counter' to the highest value // desired and are working your way backwards. // Here is where you have the code that would have been executed // in the for() loop. Breaking out of it or doing a 'continue' // equivalent can be done with using 'return;' or canceling // processing entirely can be done by setting a global var // to indicate the process is cancelled, then doing a 'return;', as in // 'bCancelOut = true; return;'. Then in func_b() you would be evaluating // bCancelOut at the top to see if it was true. If so, you'd just exit from // func_b() with a 'return;' } // func_c()
Matt Campbell Jun 26 '13 at 14:46 2013-06-26 14:46
source share