当多个ajax调用完成时

时间:2013-12-06 04:40:49

标签: jquery jquery-deferred .when

以下是无法按我想要的顺序运行的方案:

var masterData = {};
var tableNames = ['table1','table2','table3','table4','table5','table6'];
var pullSqlData = function(){
  tableNames.forEach(function(value) {
    if(storage.isEmpty(value)) {
      $.getJSON('http://domain.com?r=appsync/read&id='+value+ "&callback=", function(data){
        masterData[value] = data;
        storage.set(value,data);
      });
    } else {
      masterData[value] = storage.get(value);
    }
  });
};

$.when(pullSqlData()).done(function(){
console.log('all done');
});

在我四处搜索之后,我知道如果我手动执行类似

的操作,我可以上班
$.when(
$.getJSON('http://domain.com?r=appsync/read&id=table1&callback=', function(data){
        masterData[value] = data;
        storage.set(value,data);
      }),
$.getJSON('http://domain.com?r=appsync/read&id=table2&callback=', function(data){
        masterData[value] = data;
        storage.set(value,data);
      }),
//more calls
).done(function(){
console.log('all done');
});

但是我想知道是否有办法以正确的方式做事

* storage是一个HTML5 localStorage jQuery插件

3 个答案:

答案 0 :(得分:5)

由于您是动态数量的请求,请尝试

var masterData = {};
var tableNames = ['table1', 'table2', 'table3', 'table4', 'table5', 'table6'];
var pullSqlData = function () {
    var requests = [];
    tableNames.forEach(function (value) {
        if (storage.isEmpty(value)) {
            var xhr = $.getJSON('http://domain.com?r=appsync/read&id=' + value + "&callback=", function (data) {
                masterData[value] = data;
                storage.set(value, data);
            });
            requests.push(xhr)
        } else {
            masterData[value] = storage.get(value);
        }
    });
    return requests;
};

$.when.apply($, pullSqlData()).done(function () {
    console.log('all done');
});

您需要将所有ajax请求传递给$.when(),以便pullSqlData必须返回由它创建的ajax请求列表。此外,$ .when()不会将数组作为参数,因此您需要使用Function.apply()将可变数量的参数传递给它

答案 1 :(得分:0)

检查每次成功执行

var masterData = {};
var tableNames = ['table1', 'table2', 'table3', 'table4', 'table5', 'table6'];
var pullSqlData = function () {
    var requests = [];
    tableNames.forEach(function (value) {
        if (storage.isEmpty(value)) {
            var xhr = $.getJSON('http://domain.com?r=appsync/read&id=' + value + "&callback=", function (data) {
                masterData[value] = data;
                storage.set(value, data);
                check_if_all_done();
            });
            requests.push(xhr)
        } else {
            masterData[value] = storage.get(value);
        }
    });
    return requests;
};

function check_if_all_done()
{
   if(masterData.length == tableNames.length ) console.log('done');

}

答案 2 :(得分:0)

这样的事情应该按顺序发出请求:

var requests = pullSqlData() //save in array of promises each request as @ArunPJhonny

var first = requests.shift();

        $.each(requests, function (n, p) {
            first.then(function () {
                return p;
            });
        });

        first.done(function () {
            //...done
        });