javascript - simpleweather.js 中的高温/低温不正确/过时

标签 javascript

如果这是显而易见的,我很抱歉,但我只是在编码时进行修补和修改。我正在尝试构建一个天气显示页面,并且已经按照我想要的方式获得了所有内容,但是当天的最高价和最低价似乎停留在创建页面当天的数据上,并且在两天内没有更新。我是否缺少某些内容来获取最新数据?

这是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+'&deg;'+weather.units.temp+'</p>' ; 
        low = '<p>Today\'s Low: '+weather.low+'&deg; '+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 的牺牲品(这是我贫血的小博客上的一篇文章):您想要声明您正在使用的变量(citytemp 等)在 ajax 回调中使用 var

关于javascript - simpleweather.js 中的高温/低温不正确/过时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34359454/

相关文章:

javascript - 如何使用 mysql 制作 Electron 登录系统还是应该做其他事情?

javascript - 为什么 RegExp 在 IE8 上生成错误 "unexpected quantifier"?

javascript - 如何从 asp.net HyperLink 控件调用 javascript

javascript - 从语音交互 Canvas 中获取文本?

javascript - 数据表显示/隐藏带有复选框的列

javascript - 在浏览器环境中执行 1000+ 页/分钟

javascript - 使用 JavaScript 切换元素的可见性

javascript - 如何获取对象数组中某些特定属性的重复值的计数并将其存储在对象中

javascript - 创建动态类名,然后使用 Reactjs 递增值

php - 在 Twiiter Bootstrap v2 中整合 Typeahead/JSON 数据源?