我有以下嵌套的$ .each循环:
$.each(fromStopAr, function(index, value) {
$.each(toStopAr, function(key, data2) {
console.log("Checking for match against departure stop:" + value + " destination stop : " + data2);
getMatchingStops(value, data2);
});
});
循环遍历两个原点和目标停止ID的数组,并循环调用getMatchingStops函数以找到具有匹配行程的停靠点。
它工作正常,但是因为数组从最近到最远的列表有停止列表,所以我想通过我的AJAX调用获得成功后打破循环:(成功意味着我得到一个非我的php DB调用空响应。)
$.ajax({
url: 'main.php',
async:true,
data: {
action: 'getMatchingStops',
fromstopid: fromstop,
tostopid: tostop,
deptime: deptime,
aftertime: aftertime,
},
dataType: 'json',
type: 'post',
success: function(data) {
$.each(data, function(key, data) {
console.log("Matching Stops " + data.route_short_name);
});
// This is where I want to break the nested loop that is calling this function - return false doesn't work.
return false;
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("Error: "+ errorThrown);
}
正如您所看到的,我已尝试返回false,但这不起作用。
如何在第一个AJAX成功时停止$ .each循环?