我有一个 for 循环,我想这样退出:
function MyFunction() {
for (var i = 0; i < SomeCondition; i++) {
if (i === SomeOtherCondition) {
// Do some work here.
return false;
}
}
// Execute the following code after breaking out of the for loop above.
SomeOtherFunction();
}
问题是在// Do some work here.
之后语句执行时,我想退出 for 循环,但仍想执行整个 for 循环下方的代码(// Execute the following code after breaking out of the for loop above.
下方的所有内容)。
return false
语句确实退出了 for 循环,但它也退出了整个函数。我该如何解决这个问题?
最佳答案
您正在寻找 break
statement .
关于javascript退出for循环而不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10471456/