ajax json天气自动刷新数据

ajax json weather auto refresh data

本文关键字:刷新 数据 json ajax      更新时间:2023-09-26

我正在尝试进行天气自动刷新,每5秒重新加载一次更改。它第一次加载时加载得很好,但我的setinterval工作不正常。它每5秒就会出现一次,但即使做出了更改,它也不会更新我的菜单?

到目前为止,我得到的是:

 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);
 }
 }
 });
 });
 } 

它似乎不起作用。

$(document).ready(function() {
    var weather = function() {
      ... your ajax function here ....
    };
   weather();
   -- add your timer functionality here and wire it to call weather --
});

您必须将weather声明为一个函数,然后调用该函数。然后创建您的计时器,重复调用天气功能,以完成更新调用。