Jquery call another function after ajax in loop is done

后端 未结 1 1698
走了就别回头了
走了就别回头了 2021-01-28 22:50

I have an ajax function that runs inside a loop and is dependent on the amount of items in the array. How do I run another function after the ajax call is done and outside the l

相关标签:
1条回答
  • 2021-01-28 23:21

    Lets start with after all calls succeded which is quite easy:

    success: function(data){
          downloads.push(data);
          $('.loading').fadeOut("slow");
          if(downloads.length===checked.length){//suppose checked is array like
            alert("finish");
          }
     },
    

    Outside of the loop is more difficult as it isnt really a loop and it requires some modifications. You basically want to await multiple Promises (Promise.all):

    Promise.all(checked.map(function(value){
     return Promise(function(resolve){
      $.ajax({... ,success:resolve});
     });
    })).then(function(downloads){
     console.log("finished",downloads);
    });
    
    0 讨论(0)
提交回复
热议问题