I have made 4 ajax calls, it should return false (stop), when record 123456 is found. Also, record 123456 might exist in all four ajax results. However, instead of stop, the code continues to execute.
var endPoint0 = '';
var endPoint1 = '';
var endPoint2 = '';
var endPoint3 = '';
$.when(
$.ajax(endPoint0),
$.ajax(endPoint1),
$.ajax(endPoint2),
$.ajax(endPoint3)
).then(pass);
function pass(a, b, c, d){
var array = [];
array.push(a[0].guides, b[0].guides, c[0].guides, d[0].guides);
$.each(array, function(index,jsonObject){
$.each(jsonObject, function(i, item){
var e = item.program_id;
if (e == 123456) {
listing(item);
return false;
} else {
console.log('no record');
}
});
});
}
function listing () {
// display output here
}
As Per Jquery Documentation http://api.jquery.com/jquery.when/
var d1 = new $.Deferred();
var d2 = new $.Deferred();
$.when( d1, d2 ).done(function ( v1, v2 ) {
console.log( v1 ); // "Fish"
console.log( v2 ); // "Pizza"
});
d1.resolve( "Fish" );
d2.resolve( "Pizza" );
You need to mark callback as resolve. My Idea is to put your logic then and Mark it resolve and make the execution stops.
did you tried using break? since it's inside the loop.
$.each(array, function(index,jsonObject){
$.each(jsonObject, function(i, item){
var e = item.program_id;
if (e == 123456) {
listing(item);
break; //return false;
}else{
console.log('no record')
}
});
});
I think, that the problem is, that you break from the inner each
function block, however not the outer one.