How to take an action when all ajax calls in each loop success?

前端 未结 4 898
情书的邮戳
情书的邮戳 2020-12-09 07:18

I call ajax jquery function inside .each loop, and I want to do an action just when all the ajax calls inside the loop finish and success.

$(\".checked-box\"         


        
4条回答
  •  既然无缘
    2020-12-09 07:52

    Deferreds can make your work simpler.

    var deferreds = $('.checked-box').map(function(i, elem) {
      return $.ajax(params);
    });
    
    $.when.apply(null, deferreds.get()).then(function() { ... });
    

    Hope this should work.

    The concept is

    $.when(
        $.ajax( "1" ),
        $.ajax( "2" ),
        $.ajax( "3" )
    ).then( successFunc, failureFunc );
    

提交回复
热议问题