Breaking jQuery parent function of each function

I have a $ .each jQuery function sitting in a parent javascript function, how can I split a parent function into a specific index (i)?

+5
source share
5 answers

From the sound of this, you have something like this:

function outer(someParam) {
    $.each(someParam, function(i) {
        // do something with each value in someParam
    });
}

You want to return from outerwhen the inner loop reaches a certain value. You cannot do this at a time. The key point is that execution return falsefrom the callback $.eachcompletes the "loop". Then you can set the variable for conditional return if you need it:

function outer(someParam) {
    var returnNow = false;
    $.each(someParam, function(i) {
        if (i === 5) {
            returnNow = true;
            return false;
        }

        // do something with each value in someParam
    });

    if (returnNow) {
        return;
        // return immediately
    }

    // do some other action if you want to
}
+5
source

, false:

$('something').each(function() {
    if (need_to_break) {
        return false; // returning false stops the loop
    }
});

/ each , :

var $break = {};
$('something').each(function() {
    try {
        $('something').each(function() {
            $('something').each(function() {
                $('something').each(function() {
                    throw $break;
                });
            });
        });
    } catch(E) {
        if (E != $break) throw E;
    }
});

, .

Prototype.js break Enumerable.each(), .

:

var do_break = false;
$('something').each(function() {
    $('something').each(function() {
        $('something').each(function() {
            $('something').each(function() {
                do_break = true;
                return false;
            });
            if (do_break) {
                return false;
            }
        });
        if (do_break) {
            return false;
        }
    });
    if (do_break) {
        return false;
    }
});
+10

, , () , , == x, ... , , .

0

- :

function foo() {

var success = true;

$('element').each(function() {

    if (condition) {
        success = false;
        return success;
    }

});

if (!success)
   return false;

//should not get here
}
0

outerloop:
for (;;) 
{
    for (;;) 
    {
        break; /* breaks inner loop */
    } 
    for (;;) 
    {
        break outerloop; /* breaks outer loop */
    }
}
0

All Articles