Javascript 多源API回调

Javascript 多源API回调,javascript,jquery,json,api,callback,Javascript,Jquery,Json,Api,Callback,如何使用多个源实现回调函数,然后访问数组(例如JSONP) //website names or API variables to add to callbacks var callbacks = $.Callbacks(); callbacks.add(result); //然后获取回调。结果[i] 像这样的伪代码 //获取API1,API2,API3 然后使用回调对象一次处理一个API和JSON代码 试图让此代码块在结果回调中加载三个API var listAPIs = "";

如何使用多个源实现回调函数,然后访问数组(例如JSONP)

//website names or API variables to add to callbacks
var callbacks = $.Callbacks();
callbacks.add(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;

}
var listapi=”“;
var darkForecastAPI=[];
var结果=[];
var JSONAPIS=[];
$.each(numDaysAPITimes,function(a,time){
var darkForecastAPI=/*”http://api.wunderground.com/api/“+currentAPIKey+”/history_“+time+”/q/“+state+”/“+city+”。json?回调=?”*/
"http://api.forecast.io/forecast/“+currentAPIKey+”/“+city+time+”?回调=?”;
//https://api.forecast.io/forecast/APIKEY/LATITUDE,经度,时间
JSONAPIS.push($.getJSON(darkForecastAPI{
标记:“WxAPI[“+i+”],//此标记是每个JSON页面的名称吗?我尝试对其进行索引,以防这是如何引用API中JSON格式的代码。
tagmode:“任何”,
格式:“json”
}));
});
$.when(JSONAPIS).then(函数(结果){/*no log)只是一个数组*/
var EachRecipSum=0.0;
var TotalPrecisionDate=0.0;
警报(结果);
$。每个(结果、功能(d、obj){
对于(var c=0;c=0.0000&&obj.daily.data[c]。precipType==“rain”)/*编号(result.history.dailysummary.precipm,result.history.dailysummary.rain*/{
EachRecipSum=结果[d]。每日。数据[c]。精度;
TotalPrecisionDate=eachPrecisionSum+TotalPrecisionDate;///写入平均精度
警报(数学四舍五入(每平方和*10000)/10000);
$(“body”).append(“p”).text(“在过去的“+userDataDatePick+”天内,该位置每小时降雨量至少达到“+Math.round(TotalPrecisionDate*10000)/10000+”英寸”)
}else if(obj.daily.data[c].精度>=0.0000&&obj.daily.data[c].精度类型!=“rain”){
警报(“在uuu uuuu/*+result.history.dailysummary.mon+”/“+result.history.dailysummary.mday+”/“+result.history.dailysummary.year*/”);
}
}
});
});
numDaysAPITimes=0;
}

你所说的“来源”到底是什么意思?是否查找,代码是注释从未正确格式化。是的,
$。ajax
类似于
$。getJSON
,只是有更多的选项。确实有很多缩进错误。但是,最突出的问题似乎是
JSONAPIS
是一个承诺,而不是一系列承诺,并且
$。当。apply
不起作用。你需要对每个源代码做出自己的承诺,然后将所有承诺传递给
$。当
时。你仍然在做
JSONAPIS=$.getJSON(…)
,这不是数组!你需要做类似
JSONAPIS.push($.getJSON(…)的事情
,然后在循环后使用
$!