weixin_33711647 2014-12-22 07:49 采纳率: 0%
浏览 60

返回false继续执行

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
}
  • 写回答

3条回答 默认 最新

  • weixin_33694172 2014-12-22 08:12
    关注

    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.

    评论

报告相同问题?