jQuery callback for multiple ajax calls

后端 未结 14 1628
夕颜
夕颜 2020-11-22 09:18

I want to make three ajax calls in a click event. Each ajax call does a distinct operation and returns back data that is needed for a final callback. The calls themselves ar

14条回答
  •  清酒与你
    2020-11-22 09:36

    I like hvgotcodes' idea. My suggestion is to add a generic incrementer that compares the number complete to the number needed and then runs the final callback. This could be built into the final callback.

    var sync = {
     callbacksToComplete = 3,
     callbacksCompleted = 0,
     addCallbackInstance = function(){
      this.callbacksCompleted++;
      if(callbacksCompleted == callbacksToComplete) {
       doFinalCallBack();
      }
     }
    };
    

    [Edited to reflect name updates.]

提交回复
热议问题