How to exit (escape) a function from for loop inside of it?
Solution 1
Use a shared variable between the loops. Flip it to true
at the end of the each()
loop if you want to exit and at the end of the for-loop
check for it being true
. If yes, break out of it.
Solution 2
I would do it this way:
-
Create a boolean variable to check on each loop, and if the variable is true, then exit the loop (do this for each).
var exitLoop = false; $(sentences).each(function() { if(exitLoop) {return;} var s = this; alert(s); $(words).each(function(i) { if(exitLoop) {return;} if (s.indexOf(this) > -1) { alert('found ' + this); throw "Exit Error"; } }); });
Note this is not the correct use of a try-catch
as a try-catch
should strictly be used for error handling, not jumping to different sections of your code - but it will work for what you're doing.
If return
is not doing it for you, try using a try-catch
try{
$(sentences).each(function() {
var s = this;
alert(s);
$(words).each(function(i) {
if (s.indexOf(this) > -1)
{
alert('found ' + this);
throw "Exit Error";
}
});
});
}
catch (e)
{
alert(e)
}
Code taken from this answer
Solution 3
The loop can also be exited by changing the iterator value.
var arr = [1,2,3,4,5,6,7,8,9,10];
for(var i = 0;i<arr.length;i++){
console.log(i);
compute(i);
function compute(num){
//break is illegal here
//return ends only compute function
if(num>=3) i=arr.length;
}
}
Comments
-
caglaror almost 2 years
This is theoretical question to understand how many escapes (return or exit) can apply to nested loops or other controls and functions.
I confused about this because I am stuck in the code How to escape from for ... each loop and method at the same time?
I can't stop iterating over
option
s inselect
element.I tried
return
andreturn false
already, but am unsuccesful.Generally how we can do that?
function() { for (...) { if (...) { $(...).each(function() { // You have to exit outer function from here }); } } }
-
Toon Casteele about 11 yearsJust wondering to the down voter? What is incorrect about my answer?
-
caglaror about 11 yearsif the point of i wrote comment we
break;
the code it said break is Illegal at there. -
What have you tried about 11 yearsThis code works fine, there is no reason for a downvote. Care to explain?
-
caglaror about 11 yearsYes @Konstantin D this will solve my problem. But shall we say there is no direct solution to this?
-
Konstantin Dinev about 11 years@caglaror I would say yes.
-
Toon Casteele about 11 yearsI don't know if you realize, but
for each()
is not javascript code. At least not that I know of -
What have you tried about 11 yearsA try catch should be used for error handling, not for jumping to different sections of a script.
-
What have you tried about 11 years@nalply I edited, not sure what you're looking for to be honest
-
nalply about 11 years
-
Toon Casteele about 11 yearsI've said this before. Not everything on wc3schools.com is wrong. And if the link I posted contains correct information, please shut it.
-
caglaror over 6 yearsBut the nested loops? Inner loops will continue to iterate.