ajax json天气自动刷新数据
ajax json weather auto refresh data
我正在尝试进行天气自动刷新,每5秒重新加载一次更改。它第一次加载时加载得很好,但我的setinterval工作不正常。它每5秒就会出现一次,但即使做出了更改,它也不会更新我的菜单?
到目前为止,我得到的是:
var x = document.getElementById("demo");
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
function showPosition(position) {
var location = position.coords.latitude + "," + position.coords.longitude;
jQuery(document).ready(function(weather) {
$.ajax({
url : "https://api.wunderground.com/api/0ce1c4a981f7dd2a/geolookup/lang:AR/forecast/conditions/q/"+location+".json",
dataType : "jsonp",
success : function(parsed_json) {
var location = parsed_json['location']['city'];
var temp_f = parsed_json['current_observation']['temp_f'];
var weather_html = ("<h3>Results of " + parsed_json.current_observation.display_location.city +
"</h3>" + "<p>Temperature: " + parsed_json.current_observation.temp_f + "</p>" +
"<p>Current Weather: " + parsed_json.current_observation.weather + "</p>" + "<p>Wind Gusts: " +
parsed_json.current_observation.wind_mph + "mph</p>" + '<img src="http://icons.wxug.com/logos/PNG/wundergroundLogo_black_horz.png" width="200"</img>');
$('#returned_data').html(weather_html).hide().fadeIn("slow");
$(document).ready(function() {
weather(); //Get the initial weather.
setInterval(weather, 600000); //Update the weather every 10 minutes.
});
var forecast = parsed_json['forecast']['txt_forecast']['forecastday'];
for (index in forecast) {
var newForecastString = '' + forecast[index]['title'] + ' سيكون الطقس ' + forecast[index]['fcttext_metric'];
var newForecastParagraph = $('<p/>').text(newForecastString);
$(".astro").append(newForecastParagraph);
}
}
});
});
}
它似乎不起作用。
$(document).ready(function() {
var weather = function() {
... your ajax function here ....
};
weather();
-- add your timer functionality here and wire it to call weather --
});
您必须将weather声明为一个函数,然后调用该函数。然后创建您的计时器,重复调用天气功能,以完成更新调用。
相关文章:
- JQGrid使用服务器编辑后的更新数据刷新数据
- 如何刷新数据表列数据
- vue.js使用定时器自动重新加载/刷新数据
- 数据库更新后刷新数据表
- 插入数据并用新数据刷新数据网格后,关闭弹出窗口
- 如何使用复选框jQuery取消选中并刷新数据
- 刷新数据时,下拉列表以 ng-重复形式关闭
- 仅当使用 AngularJS 从服务器中找到新数据时,才自动刷新数据
- 从 json 接收的 jQuery 刷新数据
- 主干:更改集合后刷新数据表
- 谷歌折线图,刷新数据表
- 在角度.js中编辑后立即刷新数据表
- ajax json天气自动刷新数据
- 单选按钮不刷新数据
- 在设置时间后刷新数据并调用backingbean方法
- 刷新数据表时出错-无法重新初始化数据表
- 勾选复选框全选,并刷新数据表
- 如何在调用页事件后刷新数据表
- 我如何使一个html页面可打印而无需重新加载/刷新数据
- NVD3 -如何刷新数据功能,以产生新的数据点击