gpt4 book ai didi

javascript - ajax json天气自动刷新数据

转载 作者:行者123 更新时间:2023-12-03 08:08:15 26 4
gpt4 key购买 nike

我正在尝试进行天气自动刷新,每 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 --
});

您必须将天气声明为函数,然后调用该函数。然后创建计时器来重复调用天气函数,以完成您的更新调用。

关于javascript - ajax json天气自动刷新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34271533/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com