Search code examples
javascriptjqueryweather-api

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


Hi I have tried a plugin from simpleweatherjs I followed the samples there and tried to take the geolocation and auto update sample to make a geolocation that automatically updates after a few minutes the script worked fine not until it reached the interval that I have set instead it gives me [Object Object] as a result

here is the script that I have

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

can someone please check my code if what I did was wrong? or am I missing something here? please help me this is driving me crazy


Solution

  • You are calling the getWeather function from the setInterval function, but that function expects two parameters to be passed, which you don't. If you re-arrange your code as follows, it will work:

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

    You can verify this by going to this JSFiddle: http://jsfiddle.net/wdPA4/