How can I wait for set of asynchronous callback functions?

前端 未结 6 1806
感动是毒
感动是毒 2020-11-22 09:07

I have code that looks something like this in javascript:

forloop {
    //async call, returns an array to its callback
}

After ALL of those

6条回答
  •  花落未央
    2020-11-22 09:44

    You can use jQuery's Deferred object along with the when method.

    deferredArray = [];
    forloop {
        deferred = new $.Deferred();
        ajaxCall(function() {
          deferred.resolve();
        }
        deferredArray.push(deferred);
    }
    
    $.when(deferredArray, function() {
      //this code is called after all the ajax calls are done
    });
    

提交回复
热议问题