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

前端 未结 4 926
情书的邮戳
情书的邮戳 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:56

    This is one way:

    var numberOfPendingRequests = 8;
    $(".checked-box").each(function () {   
       $.ajax({
         success: function(){
            ...
            numberOfPendingRequests --;
            if(numberOfPendingRequests == 0) allDone();
         }
       });
    });
    function allDone(){
      $('.messsage').html('all requests done');
    }
    

    You can calculate the initial numberOfPendingRequests based on your number of checkboxes or start it at 0 an increment it before each ajax request, but this is the general idea.

    Hope this helps. Cheers

提交回复
热议问题