从每个循环中的javascript匿名函数获取返回值
我以前从未使用过js,现在面临一个问题,下一步该怎么做从每个循环中的javascript匿名函数获取返回值,javascript,jquery,loops,each,Javascript,Jquery,Loops,Each,我以前从未使用过js,现在面临一个问题,下一步该怎么做 function test() { $(xml).find("strengths").each(function() { $(this).each(function() { (if some condition) { //I want to break out from both each loops at
function test()
{
$(xml).find("strengths").each(function()
{
$(this).each(function()
{
(if some condition)
{
//I want to break out from both each loops at the same time here.
// And from main function too!
}
});
});
}
我知道要停止一个循环,我只需要返回false
。但是如果我有一些嵌套的,该怎么办呢?如何从主函数返回
谢谢大家 您可以使用临时变量,如下所示:
function test () {
$(xml).find("strengths").each(function() {
var cancel = false;
$(this).each(function() {
(if some condition) {
cancel = true;
return false;
}
});
if (cancel) {
return false;
}
});
}
您可以使用两个变量:
function test()
{
var toContinue = true,
toReturn;
$(xml).find("strengths").each(function()
{
$(this).each(function()
{
if("some condition")
{
toReturn = {something: "sexy there!"};
return toContinue = false;
}
});
return toContinue;
});
if(toReturn) return toReturn;
//else do stuff;
}
谢谢,这就是我需要的!