I'm using the plugin code posted at:
... which is essentailly an asynchronous timer loop:
jQuery.forEach = function (in_array, in_pause_ms, in_callback)
{
if (!in_array.length) return; // make sure array was sent
var i = 0; // starting index
bgEach(); // call the function
function bgEach()
{
if (in_callback.call(in_array[i], i, in_array[i]) !== false)
{
i++; // move to next item
if (i < in_array.length) setTimeout(bgEach, in_pause_ms);
}
}
return in_array; // returns array
};
jQuery.fn.forEach = function (in_callback, in_optional_pause_ms)
{
if (!in_optional_pause_ms) in_optional_pause_ms = 10; // default
return jQuery.forEach(this, in_optional_pause_ms, in_callback); // run it
};
With the help of this great community, I have learned that if I "return true;" or "return false;" it has the same affect of moving on to the next iteration of the loop.
What I'd like to do is also programmatically kill (destroy) the $.forEach loop call entirely, not allowing the remaining iterations to run.
How might I add a method to this plugin to allow me to kill a specific call to the plugin while not affecting other calls concurrently running calls?
I've thought about assigning the call to a variable like so:
var foo = $.forEach(json, 1000, function(idx,item) { /* do stuff */ });
...to reference specific instances later without affecting others, but how do I add a kill method and invoke it on foo? I need the equivalent of foo.abort() that's used with ajax calls.
Can it be done? If so, how?