-2

I'm using the Asynchronous each() function plugin code posted at:

jQuery Tips and Tricks

It works well but I can't seem to exit a running callback function using return; or return true;. the consquence is that when the condition to "exit" is met, it stops all remaining $.forEach "loop" execution.

Since the plugin implements a setTimeout loop approach, perhaps I just need to know how to exit an already running setTimeout callback function? BTW - I'm avoiding using for loops or $.each() to avoid browser lockup while big json processing occurs. "backgrounding" the loop significantly improves performance of the UI.

$.forEach(json, 1000, function(idx,item) {
    if(some_condition) return true; //exit this timeout iteration

    //otherwise do something
});

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
};

Thanks much!

Community
  • 1
  • 1
Inator
  • 994
  • 2
  • 14
  • 33
  • See [How do I break out of an $.each in jquery?](http://stackoverflow.com/questions/3944083/how-do-i-break-out-of-an-each-in-jquery) – Matthew Flaschen Apr 08 '12 at 21:54
  • I don't really understand the problem here, if you just `return false;` your code should stop looping over in_array (and continue on every other returned value, even `undefined`). – Niko Apr 08 '12 at 21:58
  • tried a return false; no go. You must be thinking of $.each(). See the plug code I supplied. no each() loop involved. – Inator Apr 08 '12 at 22:00
  • @Inator, both your question and title say `each`. – Matthew Flaschen Apr 08 '12 at 22:07
  • Please post the code of the callback - this line `if (in_callback.call(in_array[i], i, in_array[i]) !== false)` makes sure that if "false" gets returned the iteration stops. The problem must be somewhere else... – Niko Apr 08 '12 at 22:10
  • I've got to say that this community is very trigger happy on the down votes. I posted a link to the plugin, the actual code for the plugin, and get dinged because I used the same title as the author for the plugin itself? Amazing! – Inator Apr 08 '12 at 23:54
  • @Inator, I didn't down-vote you, but your title is confusing. – Matthew Flaschen Apr 09 '12 at 00:00

1 Answers1

0

From the docs:

We can break the $.each() loop at a particular iteration by making the callback function return false.

EDIT: This also applies to your custom forEach.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • @Inator: But did you try Matthew's solution? Take a look at this line of code from the plugin: `if (in_callback.call(in_array[i], i, in_array[i]) !== false)` If you `return false` from the callback, it seems like it'll stop the iterations. –  Apr 08 '12 at 22:00
  • Awh... but I don't want to stop the iterations... I want to continue on to the next item. – Inator Apr 08 '12 at 22:03
  • 2
    @Inator: Oh, well then any `return` will halt the current callback. Did you make the `return` conditional? If so, have you verified that the condition is met? Or if it is always stopping the iteration, then have you checked the console for errors? –  Apr 08 '12 at 22:04
  • yes I verified the condition is met. So if I read correctly, it's operating as designed then? – Inator Apr 08 '12 at 22:07
  • 3
    @Inator: If you `return` anything but `false`, it should continue on to the next iteration. If it's halting the iterations prematurely, then there's some other issue. –  Apr 08 '12 at 22:09