simpleweatherjs js插件不会更新新的天气温度,而是返回[对象对象]

时间:2014-06-23 11:31:11

标签: javascript jquery weather-api

您好我已经尝试过来自simpleweatherjs的插件我跟踪了那里的示例,并尝试将geolocationauto update示例制作一个地理位置,在几分钟后自动更新脚本工作正常,直到它达到我设置的间隔,而不是它给我[对象对象]作为结果

这是我的脚本

<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>

有人可以检查我的代码,如果我做错了吗?或者我在这里遗失了什么?请帮帮我,这让我发疯了

1 个答案:

答案 0 :(得分:2)

您正在调用getWeather函数中的setInterval函数,但该函数需要传递两个参数,您不会这样做。如果您按如下方式重新安排代码,它将起作用:

<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/