如果这是显而易见的,我很抱歉,但我只是在编码时进行修补和修改。我正在尝试构建一个天气显示页面,并且已经按照我想要的方式获得了所有内容,但是当天的最高价和最低价似乎停留在创建页面当天的数据上,并且在两天内没有更新。我是否缺少某些内容来获取最新数据?
这是js代码:
if("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(function(position) {
loadWeather(position.coords.latitude + ',' + position.coords.longitude);
});
} else {
loadweather("Danville, KY", "");
}
$(document).ready(function() {
setInterval(getWeather, 60000);
});
function loadWeather(location, woeid) {
$.simpleWeather({
location: location,
woeid: 2389342,
unit: 'f',
success: function(weather) {
city = weather.city;
temp = weather.temp+'°';
wcode = '<img class="weathericon" src="images/weathericons/' + weather.code + '.svg">';
high = '<p>Today\'s High: '+weather.high+'°'+weather.units.temp+'</p>' ;
low = '<p>Today\'s Low: '+weather.low+'° '+weather.units.temp+'</p>';
$(".location").text(city);
$(".temperature").html(temp);
$(".climate_bg").html(wcode);
$(".high").html(high);
$(".low").html(low);
},
error: function(error) {
$(".error").html('<p>' + error + '<p>');
}
});
}
最佳答案
在我看来,您似乎忘记了创建 getWeather
函数。我认为您的前几行代码应该位于其中,因此用此替换 ready
调用之前的所有内容应该可以解决它:
function getWeather() {
if("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(function(position) {
loadWeather(position.coords.latitude + ',' + position.coords.longitude);
});
} else {
loadweather("Danville, KY", "");
}
}
<小时/>
也就是说,您提供的代码正在成为 The Horror of Implicit Globals 的牺牲品(这是我贫血的小博客上的一篇文章):您想要声明您正在使用的变量(city
、temp
等)在 ajax 回调中使用 var
。
关于javascript - simpleweather.js 中的高温/低温不正确/过时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34359454/