I have the below nested $.each loop:
$.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);
});
});
It is looping through two arrays of origin and destination stop id's and looping callung the getMatchingStops function to find stops that have a matching trip.
it works fine, however as the arrays have the stops listing from closest to furthest away, I would like to break the loop once I get a success from my AJAX call bellow: (A success means that I'm getting a non empty reponse from my php DB call.)
$.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);
}
As you can see I have tried returning false, however this doesn't work.
How can I stop the $.each loops on the first AJAX sucess?