Wait until all jQuery Ajax requests are done?

后端 未结 21 1788
离开以前
离开以前 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:18

    My solution is as follows

    var request;
    ...
    'services': {
      'GetAddressBookData': function() {
        //This is the primary service that loads all addressbook records 
        request = $.ajax({
          type: "POST",
          url: "Default.aspx/GetAddressBook",
          contentType: "application/json;",
          dataType: "json"
        });
      },
    
      ...
    
      'apps': {
        'AddressBook': {
          'data': "",
          'Start': function() {
              ...services.GetAddressBookData();
              request.done(function(response) {
                trace("ajax successful");
                ..apps.AddressBook.data = response['d'];
                ...apps.AddressBook.Filter();
              });
              request.fail(function(xhr, textStatus, errorThrown) {
                trace("ajax failed - " + errorThrown);
              });
    

    Worked quite nicely. I've tried a lot of different ways of doing this, but I found this to be the simplest and most reusable. Hope it helps

提交回复
热议问题