aligassan
aligassan

Reputation: 23

ajax json weather auto refresh data

I am trying to make an weather auto refresh which is reloading for changes every 5 seconds. It loads perfectly first time on load but my setinterval is not working correctly. It goes of every 5 seconds but it doesnt update my menu even though changes has been made?

Here is what i got so far:

 var x = document.getElementById("demo");

 if (navigator.geolocation) {
 navigator.geolocation.getCurrentPosition(showPosition);
 } else { 
 x.innerHTML = "Geolocation is not supported by this browser.";
 }

 function showPosition(position) {
 var location = position.coords.latitude + "," + position.coords.longitude; 

 jQuery(document).ready(function(weather) {

 $.ajax({
 url : "https://api.wunderground.com/api/0ce1c4a981f7dd2a/geolookup/lang:AR/forecast/conditions/q/"+location+".json",
 dataType : "jsonp",

 success : function(parsed_json) {
 var location = parsed_json['location']['city'];
 var temp_f = parsed_json['current_observation']['temp_f'];

            var weather_html = ("<h3>Results of " + parsed_json.current_observation.display_location.city +
            "</h3>"  + "<p>Temperature: " + parsed_json.current_observation.temp_f + "</p>" +
            "<p>Current Weather: " + parsed_json.current_observation.weather + "</p>" + "<p>Wind Gusts: " +
            parsed_json.current_observation.wind_mph + "mph</p>" + '<img src="http://icons.wxug.com/logos/PNG/wundergroundLogo_black_horz.png" width="200"</img>');

            $('#returned_data').html(weather_html).hide().fadeIn("slow");


$(document).ready(function() {  
  weather(); //Get the initial weather.
  setInterval(weather, 600000); //Update the weather every 10 minutes.
});
 var forecast = parsed_json['forecast']['txt_forecast']['forecastday'];
 for (index in forecast) {
 var newForecastString = '' + forecast[index]['title'] + ' سيكون الطقس ' + forecast[index]['fcttext_metric'];
 var newForecastParagraph = $('<p/>').text(newForecastString);
 $(".astro").append(newForecastParagraph);
 }
 }
 });
 });
 } 

It doesn't seem to be working.

Upvotes: 0

Views: 476

Answers (1)

Gjohn
Gjohn

Reputation: 1281

$(document).ready(function() {
    var weather = function() {
      ... your ajax function here ....
    };

   weather();
   -- add your timer functionality here and wire it to call weather --
});

You have to declare weather as a function and then call the function. Then create your timer to repeatedly call the weather function in order to fulfill your update call.

Upvotes: 1

Related Questions