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'將在內部函數被調用之前返回。 –