如何运行的getJSON顺序

问题描述 投票:1回答:1

我试图嵌入天气预报在我的网页的具体日期。下面是我的测试页http://elhuracantango.com/pogoda,我不能得到正常工作的代码节选:

<script>
function b(date){
$.getJSON(url + apiKey + "/" + lati + "," + longi + ","+ date+ "?units=si&lang=ru&exclude=hourly&callback=?", 
function(data) {
 $('#weather').append(weekdays[dateobj.getDay()] + " : "+ data.daily.data[0].summary))
}
b("2015-06-25T12:00:00");
b("2015-06-26T12:00:00");
b("2015-06-27T12:00:00");
b("2015-06-28T12:00:00");
</script>

但网页上的预测并不总是得到顺序显示。

javascript jquery jsonp
1个回答
0
投票

它是一种异步操作,所以你不能保证哪一个会先返回结果。你必须使用回调/承诺处理这个。

如果要更新在时间间隔的具体天气。使用setInterval

例如:

function b(date){ 
  var result = // ajax call here.

  return result 
}


setInterval(function(){
     var html = b(new Date());
     $('#result').html(html);
}, 1000);
© www.soinside.com 2019 - 2024. All rights reserved.