Dynamic multiple Deferred jQuery Ajax calls

后端 未结 1 1284
小蘑菇
小蘑菇 2021-02-01 04:09

Using the Deferred pattern from jQuery http://api.jquery.com/jQuery.when/, I am trying to make multiple jsonp ajax calls and wait for the results before moving to the next step.

相关标签:
1条回答
  • 2021-02-01 04:53

    You can use arguments, which is a special king of object holding all arguments passed to a function

    $.when.apply($, requests).done(function () {
        console.log(arguments); //it is an array like object which can be looped
        var total = 0;
        $.each(arguments, function (i, data) {
            console.log(data); //data is the value returned by each of the ajax requests
    
            total += data[0]; //if the result of the ajax request is a int value then
        });
    
        console.log(total)
    });
    
    0 讨论(0)
提交回复
热议问题