如何在javascript中从回调函数中获取整个数据 [英] How to get whole data out of callback function in javascript

查看:121
本文介绍了如何在javascript中从回调函数中获取整个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下函数,该函数从网址获取json数据。

I have written following function which takes a json data from a url.

function getWeatherDataForCities(cityArray){
  var arrAllrecords = [];
  var toDaysTimestamp = Math.round((new Date()).getTime() / 1000) - (24*60*60);
  for(var i in cityArray){

    for(var j=1; j<=2; j++){
        var jsonurl = "http://api.openweathermap.org/data/2.5/history/city?q="+cityArray[i]+"&dt="+toDaysTimestamp;

        $.ajax({
            url: jsonurl,
            dataType: "jsonp",
            mimeType: "textPlain",
            crossDomain: true,
            contentType: "application/json; charset=utf-8",
            success: function(data){
              arrAllrecords[j]["cityName"] = data.list[0].city.name;
              arrAllrecords[j]["weather"] = data.list[0].weather[0].description;

        } });
        toDaysTimestamp = toDaysTimestamp - (24*60*60);
    }   
  }

  return arrAllrecords;  //returning arrAllrecords
}

$(document ).ready(function() {

  var cityArray = new Array();
  cityArray[0] = "pune";
  var arrAllRecords = getWeatherDataForCities(cityArray);
  //Print All records returned by getWeatherDataForCities(cityArray);
});

我在上面的代码中写了一些评论。我调用了 getWeatherDataForCities function 返回 url 中的所有记录。我已声明 getWeatherDataForCities <函数中的/ code> 数组我希望在该数组中添加所有返回的记录。我已经尝试过如上所述但没有任何内容进入数组

I have written some comments in above code.I have called getWeatherDataForCities function which returns all records from url.I have declared getWeatherDataForCities array in function.I want to add all returned records in that array.I have tried as above but nothing is getting into array.


在控制台中显示j和arrAllrecords未定义。

In console showing j and arrAllrecords are undefined.

如何从回调函数获取数组中的所有记录?

你回复将受到高度赞赏

推荐答案

您的 getWeatherDataForCities 功能因为ajax操作是异步的,所以不会返回任何内容。您需要使用回调。

Your getWeatherDataForCities function won't return anything because ajax operations are asynchronous. You need to use a callback for that.

修改您的函数以接受回调函数:

Modify your function to accept a callback function:

function getWeatherDataForCities(cityArray, callback){
    var arrAllrecords = [];
    var toDaysTimestamp = Math.round((new Date()).getTime() / 1000) - (24*60*60);
    for(var i in cityArray){

       for(var j=1; j<=2; j++){
           var jsonurl = "http://api.openweathermap.org/data/2.5/history/city?q="+cityArray[i]+"&dt="+toDaysTimestamp;

            $.ajax({
                url: jsonurl,
                dataType: "jsonp",
                mimeType: "textPlain",
                crossDomain: true,
                contentType: "application/json; charset=utf-8",
                success: function(data){
                arrAllrecords[j]["cityName"] = data.list[0].city.name;
                arrAllrecords[j]["weather"] = data.list[0].weather[0].description;
                // call the callback here
                callback(arrAllrecords);
               } 
            });
           toDaysTimestamp = toDaysTimestamp - (24*60*60);
       }   
   }
}

并像这样使用它:

$(document ).ready(function() {
  var cityArray = new Array();
  cityArray[0] = "pune";
   getWeatherDataForCities(cityArray, function(arrAllrecords) {
       // Do something with your data
   });
});

这篇关于如何在javascript中从回调函数中获取整个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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