jQuery callback for multiple ajax calls

后端 未结 14 1968
夕颜
夕颜 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: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);
            };
        };
    })();
    

提交回复
热议问题