Wait until all jQuery Ajax requests are done?

后端 未结 21 1800
离开以前
离开以前 2020-11-21 04:39

How do I make a function wait until all jQuery Ajax requests are done inside another function?

In short, I need to wait for all Ajax requests to be done before I exe

21条回答
  •  独厮守ぢ
    2020-11-21 05:27

    I found simple way, it using shift()

    function waitReq(id)
    {
      jQuery.ajax(
      {
        type: 'POST',
        url: ajaxurl,
        data:
        {
          "page": id
        },
        success: function(resp)
        {
          ...........
          // check array length if not "0" continue to use next array value
          if(ids.length)
          {
            waitReq(ids.shift()); // 2
          )
        },
        error: function(resp)
        {
          ....................
          if(ids.length)
          {
            waitReq(ids.shift());
          )
        }
      });
    }
    
    var ids = [1, 2, 3, 4, 5];    
    // shift() = delete first array value (then print)
    waitReq(ids.shift()); // print 1
    

提交回复
热议问题