jQuery callback for multiple ajax calls

后端 未结 14 1621
夕颜
夕颜 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:30

    I came across this problem today, and this was my naive attempt before watching the accepted answer.

    <script>
        function main() {
            var a, b, c
            var one = function() {
                if ( a != undefined  && b != undefined && c != undefined ) {
                    alert("Ok")
                } else {
                    alert( "¬¬ ")
                }
            }
    
            fakeAjaxCall( function() {
                a = "two"
                one()
            } )
            fakeAjaxCall( function() {
                b = "three"
                one()
            } )
            fakeAjaxCall( function() {
                c = "four"
                one()
            } )
        }
        function fakeAjaxCall( a ) {
            a()
        }
        main()
    </script>
    
    0 讨论(0)
  • 2020-11-22 09:32

    It's not jquery (and it appears jquery has a workable solution) but just as another option....

    I've had similar problems working heavily with SharePoint web services - you often need to pull data from multiple sources to generate input for a single process.

    To solve it I embedded this kind of functionality into my AJAX abstraction library. You can easily define a request which will trigger a set of handlers when complete. However each request can be defined with multiple http calls. Here's the component (and detailed documentation):

    DPAJAX at DepressedPress.com

    This simple example creates one request with three calls and then passes that information, in the call order, to a single handler:

        // The handler function 
    function AddUp(Nums) { alert(Nums[1] + Nums[2] + Nums[3]) }; 
    
        // Create the pool 
    myPool = DP_AJAX.createPool(); 
    
        // Create the request 
    myRequest = DP_AJAX.createRequest(AddUp); 
    
        // Add the calls to the request 
    myRequest.addCall("GET", "http://www.mysite.com/Add.htm", [5,10]); 
    myRequest.addCall("GET", "http://www.mysite.com/Add.htm", [4,6]); 
    myRequest.addCall("GET", "http://www.mysite.com/Add.htm", [7,13]); 
    
        // Add the request to the pool 
    myPool.addRequest(myRequest); 
    

    Note that unlike many of the other solutions (including, I believe the "when" solution in jquery) provided this method does not force single threading of the calls being made - each will still run as quickly (or as slowly) as the environment allows but the single handler will only be called when all are complete. It also supports the setting of timeout values and retry attempts if your service is a little flakey.

    I've found it insanely useful (and incredibly simple to understand from a code perspective). No more chaining, no more counting calls and saving output. Just "set it and forget it".

    0 讨论(0)
  • 2020-11-22 09:33

    Here is a callback object I wrote where you can either set a single callback to fire once all complete or let each have their own callback and fire them all once all complete:

    NOTICE

    Since jQuery 1.5+ you can use the deferred method as described in another answer:

      $.when($.ajax(), [...]).then(function(results){},[...]);
    

    Example of deferred here

    for jQuery < 1.5 the following will work or if you need to have your ajax calls fired at unknown times as shown here with two buttons: fired after both buttons are clicked

    [usage]

    for single callback once complete: Working Example

    // initialize here
    var requestCallback = new MyRequestsCompleted({
        numRequest: 3,
        singleCallback: function(){
            alert( "I'm the callback");
        }
    });
    
    //usage in request
    $.ajax({
        url: '/echo/html/',
        success: function(data) {
            requestCallback.requestComplete(true);
        }
    });
    $.ajax({
        url: '/echo/html/',
        success: function(data) {
            requestCallback.requestComplete(true);
        }
    });
    $.ajax({
        url: '/echo/html/',
        success: function(data) {
            requestCallback.requestComplete(true);
        }
    });
    

    each having their own callback when all complete: Working Example

    //initialize 
    var requestCallback = new MyRequestsCompleted({
        numRequest: 3
    });
    
    //usage in request
    $.ajax({
        url: '/echo/html/',
        success: function(data) {
            requestCallback.addCallbackToQueue(true, function() {
                alert('Im the first callback');
            });
        }
    });
    $.ajax({
        url: '/echo/html/',
        success: function(data) {
            requestCallback.addCallbackToQueue(true, function() {
                alert('Im the second callback');
            });
        }
    });
    $.ajax({
        url: '/echo/html/',
        success: function(data) {
            requestCallback.addCallbackToQueue(true, function() {
                alert('Im the third callback');
            });
        }
    });
    

    [The Code]

    var MyRequestsCompleted = (function() {
        var numRequestToComplete, requestsCompleted, callBacks, singleCallBack;
    
        return function(options) {
            if (!options) options = {};
    
            numRequestToComplete = options.numRequest || 0;
            requestsCompleted = options.requestsCompleted || 0;
            callBacks = [];
            var fireCallbacks = function() {
                alert("we're all complete");
                for (var i = 0; i < callBacks.length; i++) callBacks[i]();
            };
            if (options.singleCallback) callBacks.push(options.singleCallback);
    
            this.addCallbackToQueue = function(isComplete, callback) {
                if (isComplete) requestsCompleted++;
                if (callback) callBacks.push(callback);
                if (requestsCompleted == numRequestToComplete) fireCallbacks();
            };
            this.requestComplete = function(isComplete) {
                if (isComplete) requestsCompleted++;
                if (requestsCompleted == numRequestToComplete) fireCallbacks();
            };
            this.setCallback = function(callback) {
                callBacks.push(callBack);
            };
        };
    })();
    
    0 讨论(0)
  • 2020-11-22 09:33

    I found an easier way to do it without the need of extra methods that arrange a queue.

    JS

    $.ajax({
      type: 'POST',
      url: 'ajax1.php',
      data:{
        id: 1,
        cb:'method1'//declaration of callback method of ajax1.php
      },
      success: function(data){
      //catching up values
      var data = JSON.parse(data);
      var cb=data[0].cb;//here whe catching up the callback 'method1'
      eval(cb+"(JSON.stringify(data));");//here we calling method1 and pass all data
      }
    });
    
    
    $.ajax({
      type: 'POST',
      url: 'ajax2.php',
      data:{
        id: 2,
        cb:'method2'//declaration of callback method of ajax2.php
      },
      success: function(data){
      //catching up values
      var data = JSON.parse(data);
      var cb=data[0].cb;//here whe catching up the callback 'method2'
      eval(cb+"(JSON.stringify(data));");//here we calling method2 and pass all data
      }
    });
    
    
    //the callback methods
    function method1(data){
    //here we have our data from ajax1.php
    alert("method1 called with data="+data);
    //doing stuff we would only do in method1
    //..
    }
    
    function method2(data){
    //here we have our data from ajax2.php
    alert("method2 called with data="+data);
    //doing stuff we would only do in method2
    //..
    }
    

    PHP (ajax1.php)

    <?php
        //catch up callbackmethod
        $cb=$_POST['cb'];//is 'method1'
    
        $json[] = array(
        "cb" => $cb,
        "value" => "ajax1"
        );      
    
        //encoding array in JSON format
        echo json_encode($json);
    ?>
    

    PHP (ajax2.php)

    <?php
        //catch up callbackmethod
        $cb=$_POST['cb'];//is 'method2'
    
        $json[] = array(
        "cb" => $cb,
        "value" => "ajax2"
        );      
    
        //encoding array in JSON format
        echo json_encode($json);
    ?>
    
    0 讨论(0)
  • 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.]

    0 讨论(0)
  • 2020-11-22 09:37

    Looks like you've got some answers to this, however I think there is something worth mentioning here that will greatly simplify your code. jQuery introduced the $.when in v1.5. It looks like:

    $.when($.ajax(...), $.ajax(...)).then(function (resp1, resp2) {
        //this callback will be fired once all ajax calls have finished.
    });
    

    Didn't see it mentioned here, hope it helps.

    0 讨论(0)
提交回复
热议问题