js插件不更新新的天气温度,而是返回[object object]

simpleweatherjs js plugin doesnt update new weather temp but instead returns [object object]

本文关键字:object 温度 返回 插件 更新 js      更新时间:2023-09-26

嗨,我已经尝试了一个插件从simpleweatherjs我跟着那里的样本,并试图采取地理位置和自动更新的样本,使地理位置自动更新几分钟后脚本工作良好,直到它达到间隔,我已经设置,而不是它给我[对象对象]作为结果

这是我的脚本

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="jquery.simpleWeather.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {  
  navigator.geolocation.getCurrentPosition(function(position) {
    getWeather(position.coords.latitude+','+position.coords.longitude); //load weather using your lat/lng coordinates
  }); 
});
$(function(){
  getWeather();
    setInterval(getWeather, 6000);
});
function getWeather(location, woeid) {
  $.simpleWeather({
    location: location,
    woeid: woeid,
    unit: 'c',
    success: function(weather) {
    html = '<h2>'+weather.temp+'&deg;'+weather.units.temp+'</h2>';
      html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
      html += '<li class="currently">'+weather.currently+'</li></ul>';
       $("#weather").html(html);
    },
    error: function(error) {
      $("#weather").html('<p>'+error+'</p>');
    }
  });
} 
</script>
<div id="weather"></div>

有人可以检查我的代码,如果我做错了吗?还是我遗漏了什么?请帮帮我,这快把我逼疯了

您正在从setInterval函数调用getWeather函数,但是该函数期望传递两个参数,而您没有。如果按照下面的方式重新排列代码,它就可以工作了:

<script type="text/javascript">
    $(document).ready(function() {
        getWeather();
        setInterval(getWeather, 6000);
    });
    function getWeather() {
        navigator.geolocation.getCurrentPosition(function(position) {
            var location = (position.coords.latitude + ',' + position.coords.longitude);
            var woeid = undefined;
            $.simpleWeather({
                location: location,
                woeid: woeid,
                unit: 'c',
                success: function(weather) {
                    html = '<h2>' + weather.temp + '&deg;' + weather.units.temp + '</h2>';
                    html += '<ul><li>' + weather.city + ', ' + weather.region + '</li>';
                    html += '<li class="currently">' + weather.currently + '</li></ul>';
                    $("#weather").html(html);
                },
                error: function(error) {
                    $("#weather").html('<p>' + error + '</p>');
                }
            });
        });
    }
</script>
<div id="weather"></div>

您可以通过以下JSFiddle来验证:http://jsfiddle.net/wdPA4/