2013-10-28 153 views
0

我有一个天气栏设置为每10秒使用setInterval和.load加载一个名为data.php的php文件。我可以在Firebug中看到setInterval正在工作,它看起来像负载太多,但是当它运行时,它不会获得data.php文件的最新版本。JQuery加载不替换内容

的index.php

<div id="weather"></div> 

<script> 
    var auto_refresh = setInterval(
    function() 
    { 
     $('#weather').load('assets/fb_weather/data.php'); 
    }, 10000); // refresh every 10000 milliseconds 
</script> 

data.php

<div id="fb_weather_wrapper"> 
    <p id="fb_weather_today">Current</p> 
    <div id="fb_weather_top"> 
     <div class="one-half">Mt. Carmel, IL</div> 
     <div class="one-half"><?=$today_date?></div> 
     <div class="clear"></div> 
    </div> 
    <div class="fb_weather_icons fb_weather_icons_<?=$cond->icon?>"></div> 
    <p id="fb_weather_temp"><? echo round($cond->temp_f); ?>&deg;</p> 
    <p class="fb_weather_cond"><?=$cond->weather?></p> 

    <div id="fb_weather_forecast"> 
     <ul> 
      <? foreach($forecast->forecast->simpleforecast->forecastdays->forecastday as $day) 
      { ?> 
      <li class="fb_weather_day"> 
       <div class="fb_weather_icons_small fb_weather_icons_small_<?=$day->icon?>"></div> 
       <p class="fb_weather_forecast_temp"><?=$day->high->fahrenheit?>&deg;<br /> 
       <p><?=$day->date->weekday_short?></p> 
       <em class="clear"></em> 
      </li> 
      <? } ?> 
       <em class="clear"></em> 
      </li> 

     </ul> 
    </div> 
</div> 
+0

听起来你可能是可怕的高速缓存的受害者。你有没有尝试添加一个时间戳到你打电话的URL? ''('#weather')。load('assets/fb_weather/data.php?timestamp = ');' – Jonathon

+0

另外,要小心,如果服务器上的某些东西坏了,这只会持续运行,每10秒,无论前一个请求是否成功,一个快速解决方案是使用'setTimeout'并重新启动回调函数,nice和递归。 – tymeJV

回答

0

你必须删除您的.load()方法,使一个Ajax请求:

$.post('assets/fb_weather/data.php','datastring/dataArray',function(response){ 
$('#weather').load('assets/fb_weather/data.php'); 
}); 

数据字符串或数组是t他看重你要发送到页面,你可以得到这个值,你的PHP页面

,如:

print_r($_POST);