如何在所有getjson调用完成时将对象数组传递给另一个函数?

时间:2015-10-02 19:57:24

标签: javascript arrays ajax getjson

我必须做一些getjson调用才能从谷歌电子表格中获取一些数据并将我需要的数据存储到数组中并过滤数组并显示数据。我的当前代码有一段时间大量的数据在我的第一个getjson完成之前传递数组,所以我最终得到了数组中不完整的数据!

有没有一种方法,我的最后一个getjson只有在所有先前的getjson调用完成(包括它自己)之后才将数组发送到另一个函数?之后将数组传递给另一个函数?

    <script>
        var files = new Array();

           function pushtoArray(){

        //first getjson call
        var url1 = "https://spreadsheets.google.com/feeds/list/xxxxx/xxxxx/public/values?alt=json"; 
            $.getJSON(url1, function(data) {

              var entry = data.feed.entry;

              $(entry).each(function(){
                // Column names are name, age, etc.
            count++;
             files.push({ url: this.gsx$url.$t, filename: this.gsx$name.$t });



              });

            alert(files.length);
            print_r(files);
            console.log(files);
            });//end of ajax call

        //second getjson call
    var url2 = "https://spreadsheets.google.com/feeds/list/xxxxx/xxxxx/public/values?alt=json"; 
            $.getJSON(url2, function(data) {

              var entry = data.feed.entry;

              $(entry).each(function(){
                // Column names are name, age, etc.
            count++;
             files.push({ url: this.gsx$url.$t, filename: this.gsx$name.$t });


              });

            alert(files.length);
           passArray(files);
            console.log(files);
            });//end of ajax call


            };//end of function


    function passArray(files){ 
    alert(files.length);
    console.log(files);

   // do some data filtering from array passed

        </javascript>
    <body onload="pushtoArray()">

编辑我改变了

var entry1 = resultFromUrl1.feed.entry;
        var entry2 = resultFromUrl2.feed.entry;

 var entry1 = resultFromUrl1[0].feed.entry;
    var entry2 = resultFromUrl2[0].feed.entry;

它修正了错误!

1 个答案:

答案 0 :(得分:2)

使用Promises。

var promiseA = $.getJSON(urla..);
var promiseB = $.getJSON(urlb..);

$.when(promiseA, promiseB).then(function(resultA,resultB){
//do whatever you want with results.

});
编辑:这是您的代码编辑,如果是var entry = ...可能是个问题,那么只需查看debug中的entry1和entry2:

var files = new Array();

function pushtoArray() {

    //first getjson call
    var url1 = "https://spreadsheets.google.com/feeds/list/xxxxx/xxxxx/public/values?alt=json";
    var url1Promise = $.getJSON(url1, function (data) {
        console.log("url1 success");
    });//end of ajax call

    //second getjson call
    var url2 = "https://spreadsheets.google.com/feeds/list/xxxxx/xxxxx/public/values?alt=json";
    var url2Promise = $.getJSON(url2, function (data) {
        console.log("url2 success");
    });//end of function

    $.when(url1Promise, url2Promise).then(function (resultFromUrl1, resultFromUrl2) {
        var entry1 = resultFromUrl1.feed.entry;
        var entry2 = resultFromUrl2.feed.entry;

        var entry = entry1.concat(entry2);

        $(entry).each(function () {
            // Column names are name, age, etc.
            count++;
            files.push({ url: this.gsx$url.$t, filename: this.gsx$name.$t });
                    alert(files.length);
                    print_r(files);
                    console.log(files);


        });


    });