Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery - Abort each function

Tags:

jquery

each

how can I abort the jquery each function?

$.each(big_test_object, function(i)
{
   // some code

});
like image 436
Peter Avatar asked Sep 23 '10 08:09

Peter


People also ask

How can I break exit from a each () function in jQuery?

each(function() { // Code // To escape from this block based on a condition: if (something) return false; }); From the documentation of the each method: Returning 'false' from within the each function completely stops the loop through all of the elements (this is like using a 'break' with a normal loop).

How do you end a forEach loop?

You can put any number of Exit For statements in a For Each loop. When used within nested For Each loops, Exit For causes execution to exit the innermost loop and transfers control to the next higher level of nesting. Exit For is often used after an evaluation of some condition, for example, in an If ... Then ...

How do you stop a function call in jQuery?

jQuery stop() Method The stop() method stops the currently running animation for the selected elements.


2 Answers

from the jquery each documentation

We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

$.each(big_test_object, function(i)
{

   if(i == 'yourvalue') {
          return false;
   }

});
like image 132
Tim Avatar answered Oct 21 '22 12:10

Tim


you should return false in your callback

cheers

like image 38
Pedro Gil Avatar answered Oct 21 '22 11:10

Pedro Gil