多个源API回调

时间:2015-05-05 04:24:27

标签: javascript jquery json api callback

如何实现具有多个源的回调函数,然后访问数组,例如JSONP?

//website names or API variables to add to callbacks
var callbacks = $.Callbacks();
callbacks.add(result);

//然后获取callback.result [i] 像这样的伪代码......?

//获取API1,API2,API3  然后使用回调对象一次处理一个API和JSON代码。

尝试让此代码块在结果回调中加载三个API

  var listAPIs = "";
    var darkForecastAPI = [];
    var result = [];
    var JSONAPIS = [];

    $.each(numDaysAPITimes, function(a, time) {
        var darkForecastAPI = /*"http://api.wunderground.com/api/" + currentAPIKey + "/history_" + time + "/q/" + state + "/" + city +".json?callback=?"; */
            "http://api.forecast.io/forecast/" + currentAPIKey + "/" + city + time + "?callback=?";
        //https://api.forecast.io/forecast/APIKEY/LATITUDE,LONGITUDE,TIME
        JSONAPIS.push($.getJSON(darkForecastAPI, {
            tags: "WxAPI[" + i + "]", //Is this tag the name of each JSON page? I tried to index it incase this is how to refer to the JSON formatted code from the APIs.
            tagmode: "any",
            format: "json"
        }));
    });
    $.when(JSONAPIS).then(function(result) { /*no log simply an array */
        var eachPrecipSum = 0.0;
        var totalPrecipSinceDate = 0.0;
        alert(result);

        $.each(result, function(d, obj) {
            for (var c = 0; c <= obj.daily.data.length - 1; c++) {
                if (obj.daily.data[c].precipIntensity >= 0.0000 && obj.daily.data[c].precipType == "rain") /*Number(result.history.dailysummary.precipm, result.history.dailysummary.rain*/ {
                    eachPrecipSum = result[d].daily.data[c].precipIntensity;
                    totalPrecipSinceDate = eachPrecipSum + totalPrecipSinceDate; ///Write mean precip
                    alert(Math.round(eachPrecipSum * 10000) / 10000);
                    $("body").append("p").text("There has been as least a total of " + Math.round(totalPrecipSinceDate * 10000) / 10000 + " inches per hour of rain at the location in the last " + userDataDatePick + " days")

                } else if (obj.daily.data[c].precipIntensity >= 0.0000 && obj.daily.data[c].precipType != "rain") {
                    alert("There is was no rain on ____" /*+ result.history.dailysummary.mon + "/" + result.history.dailysummary.mday + "/" + result.history.dailysummary.year*/ );
                }
            }
        });
    });
    numDaysAPITimes = 0;

}

0 个答案:

没有答案