loop: function(cb) {
var intervalId;
var cycle = function() {
var ret = cb();
if(ret == false) {
clearInterval(intervalId);
}
}
intervalId = setInterval(cycle, 0);
}
...
// let's do some work with our interval based loop
var isProcessing = true;
loop(function() {
if(isProcessing) {
// do some work now
// remember to set isProcessing to false at some point
// note that you can replace it with any other sort of condition as well...
isProcessing = false;
// add progress cb here if you want to
return true; // let's continue executing the loop
}
// add done cb here if you want to
return false; // we're done, let's get outta here
});
function process(progressCb, readyCb) {
var isProcessing = true;
var intervalId;
var exec = function() {
if(isProcessing) {
// do some processing now, set isProcessing to false once you are done
...
// pass progress status here if you want to
progressCb();
}
else {
// time to kill the interval. you served us well
clearInterval(intervalId);
readyCb();
}
}
// execute interval as often as possible, hence zero
intervalId = setInterval(exec, 0);
}