试图形成从$ .getJSON的多种来源回调的结果阵列() [英] Trying to form array of callback results from multiple sources of $.getJSON()

查看:196
本文介绍了试图形成从$ .getJSON的多种来源回调的结果阵列()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code将处理来自darkForecastAPIArray [],它进入$ .getJSON()调用数组,但它只会处理其他调用索引到$ .getJSON返回后一个回调()。打完code座做,我不能API请求的每个实例进入阵列,它说APIResults.push(),它完成呼叫到达数组,然后离开只有一个回调在返回之前。任何建议如何获得的所有从结果呼叫的成以下APIResults []数组?整个剧本的工作有点像多个索引页面JSON然后将它们放置到一个数组来读取JSON的每一页的形式。为了简化这似乎是完成$ .getJSON方法范围再做回调一次。

  $。每个(numDaysAPITimes,函数(时间){
    darkForecastAPIArray.push(darkForecastAPI = /*\"http://api.wunderground.com/api/+ currentAPIKey +/ history_+时间+/ Q /+状态+/+城市+。JSON?回调=; * /
        http://api.forecast.io/forecast/+ currentAPIKey +/+城市+时间+)?回调=?;
    的console.log(darkForecastAPI);
});
//https://api.forecast.io/forecast/APIKEY/LATITUDE,LONGITUDE,TIME
//$.each(darkForecastAPIArray,功能(B,APICallRequest){
    VAR deferreds = $ .MAP(darkForecastAPIArray,功能(URL,指数){
        返回$ .getJSON(URL,{
            标签:WxAPI [+指数+],
            tagmode:任何,
            格式:JSON
        });
    });
    $ .when.apply($,deferreds)。然后(功能(结果){
        $。每个(结果,功能(索引,数据){
            // 做一点事    APIResults.push(数据);
    的console.log(指数);
    的console.log(数据);        为(C = 0;℃下= data.daily.data.length - 1; C + = 1){
            如果(data.daily.data [C] precipIntensity方式> = 0.0000和放大器;&安培; data.daily.data [C] precipType ===雨)/*Number(result.history .dailysummary。precipm,result.history.dailysummary.rain * / {
                每个precipSum = data.daily.data [C] precipIntensity。
                总precipSinceDate =每precipSum +总precipSinceDate; ///写的意思是preCIP
                警报(Math.round(各precipSum * 10000)/ 10000);
                $(身体)。追加(P)。文本(已经有至少为总的每个雨每小时+ Math.round(总precipSinceDate * 10000)/ 10000 +英寸的位置中的最后一个+ userDataDatePick +天);            }否则如果(data.daily.data [C] precipIntensity方式> = 0.0000和放大器;&安培;!data.daily.data [C] precipType ==雨){
                警报(有是在____,雨点小/ * + result.history.dailysummary.mon +/+ result.history.dailysummary.mday +/+ result.history.dailysummary.year * /);
              }
        }
    });
         });numDaysAPITimes = 0;
}


解决方案

您在您的评论在正确的轨道有关使用这个食谱。像这样的东西应该工作:

  VAR deferreds = $ .MAP(urlArray,功能(URL,指数){
    返回$ .getJSON(URL,{
        标签:WxAPI [+指数+],
        tagmode:任何,
        格式:JSON
    });
});
$ .when.apply($,deferreds)。然后(功能(结果){
    $。每个(结果,功能(索引,数据){
         // 做一点事
    });
 });

The code will process an array of calls from darkForecastAPIArray[] which go into the $.getJSON() but it will only return a single callback after processing the other calls indexed into the $.getJSON(). So after the code block is done I cannot get each instance of API requests into the array where it says APIResults.push(), it finishes the calls before it reaches the array and then it leaves only one callback on return. Any suggestions how to get all calls from the "result" into the following APIResults[] array? The whole script works somewhat like a form of indexing multiple JSON pages then placing them into an array to read each page of JSON. To simplify it appears to be finishing the $.getJSON method scope then do the callback once.

$.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;
}

解决方案

You are on the right track in your comment about using this recipe. Something like this should work:

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
    });
 });

这篇关于试图形成从$ .getJSON的多种来源回调的结果阵列()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆