function outer() {
$(['hi', 'there']).each(function(idx, e) {
console.log(e);
return;
});
alert("I don't want to be called");
}
function outer() {
$.get('http://test.com', function (data) {
console.log(data)
return; // I want to terminate the entire outer() function here.
});
alert("I don't want to be called");
}
什么是在这种情况下突破嵌套函数的约定?当使用for
循环时,在它们内部返回会终止包含它们的整个函数。然而,因为$.each()
是一个单独的函数调用,所以从它返回的只是自己而不是外部函数。我可以简单地return
两次,一旦进入内部,一次在外面,但我不知道我将如何处理$.ajax()
,因为只有当我获得成功的响应时才需要终止函数。从嵌套函数返回
我不完全确定,但尝试'返回false;' – Sheraff
你不能这样做,因为ajax的异步性质 –
简短的回答:你不能。在第一个例子中可以有一个解决方法,但是'$ .get'是异步的,并且'outer'将在内部函数被调用之前返回。 –