javascript - 尝试从 $.getJSON() 的多个源形成回调结果数组

标签 javascript arrays json callback weather-api

该代码将处理来自 darkForecastAPIArray[] 的调用数组,这些调用将进入 $.getJSON(),但在处理索引到 $.getJSON() 的其他调用后,它只会返回一个回调。因此,在代码块完成后,我无法将 API 请求的每个实例放入 APIResults.push() 所在的数组中,它会在到达数组之前完成调用,然后在返回时只留下一个回调。有什么建议如何将“结果”中的所有调用放入以下 ​​APIResults[] 数组中吗?整个脚本的工作原理有点像索引多个 JSON 页面,然后将它们放入数组中以读取每个 JSON 页面的形式。为了简化,它似乎完成了 $.getJSON 方法范围,然后执行回调一次。

$.each(numDaysAPITimes, function(a, time) {
    darkForecastAPIArray.push(darkForecastAPI = /*"http://api.wunderground.com/api/" + currentAPIKey + "/history_" + time + "/q/" + state + "/" + city +".json?callback=?"; */
        "http://api.forecast.io/forecast/" + currentAPIKey + "/" + city + time + "?callback=?");
    console.log(darkForecastAPI);
});
//https://api.forecast.io/forecast/APIKEY/LATITUDE,LONGITUDE,TIME
//$.each(darkForecastAPIArray, function(b, APICallRequest) {
    var deferreds = $.map(darkForecastAPIArray, function(url, index) {
        return $.getJSON(url, {
            tags: "WxAPI[" + index + "]",
            tagmode: "any",
            format: "json"
        });
    });
    $.when.apply($, deferreds).then(function(results) {
        $.each(results, function(index, data) {
            // do something

    APIResults.push(data);
    console.log(index);
    console.log(data);

        for (c = 0; c <= data.daily.data.length - 1; c += 1) {
            if (data.daily.data[c].precipIntensity >= 0.0000 && data.daily.data[c].precipType === "rain") /*Number(result.history.dailysummary.precipm, result.history.dailysummary.rain*/ {
                eachPrecipSum = data.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 (data.daily.data[c].precipIntensity >= 0.0000 && data.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;
}

最佳答案

您关于使用this recipe的评论是正确的。 。像这样的东西应该有效:

var deferreds = $.map(urlArray, function(url, index) {
    return $.getJSON(url, {
        tags: "WxAPI[" + index + "]",
        tagmode: "any",
        format: "json"
    });
});
$.when.apply($, deferreds).then(function(results) {
    $.each(results, function(index, data) {
         // do something
    });
 });

关于javascript - 尝试从 $.getJSON() 的多个源形成回调结果数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30091533/

相关文章:

javascript - jQuery 相当于 Prototype collect 和 map 函数?

java - Java中反转数组的程序

javascript - JSON.parse 错误是用双引号字符串给出错误

javascript - 获得正确值(value)的更好方法

php - 来自 PHP 的 Backbone JSON 请求问题

javascript - 使用 Netlify 将环境变量导入 HTML

javascript - 如何检查变量值是否包含在 JavaScript 数组中

d3.js - 使 d3.js 与 ie 兼容

javascript - 我怎样才能在 HTML 中运行它?

javascript - 如何改变 <div> 中元素的顺序?