2011-08-24 75 views
1

在php和jQueryMobile中編寫代碼。當刷新時間改變時,如何讓jQuery Mobile動態刷新div?

我試圖讓一個div刷新基於可變 refreshTime

<div id="dynamicContent"> 
<em><?= $this->subtitle ?></em> 
<img src="<?= $this->image ?>"/> 
<em><?= $this->synopsis ?></em> 
<br/> set refresh: <?= $this->refreshTime?> time on server: <?= date("H:i",time());?> 
<h2><a href="<?=$this->irrelaventLink ?>"> More Details</a></h2> 
</div> 

<script> 
setTimeout(function() { 
       $('#dynamicContent').load('/url/that/generates/plain/html/'); 
      },<?= $this->refreshTime ?>*1000); 
</script> 

所以這個代碼將觸發一次刷新DIV DynamicContent類 refreshTime過去後。 問題是,我想繼續使用新值刷新時間 refreshTime,一旦刷新發生就會設置它。

我已經搜遍了網絡,尋找一個jQuery Mobile的方式來做到這一點,雖然如果jQuery的方式工作,我很高興嘗試一下,看看在手機上看起來像什麼。

注意行:

<br/> set refresh: <?= $this->refreshTime?> time on server: <?= date("H:i",time());?> 

僅僅是有用於調試

任何意見/想法/讚賞的答案。

乾杯

回答

3

您已經將刷新時間硬編碼到setTimeout函數中。它看起來是動態的,但僅僅是因爲該值是在頁面生成時設置的,並且PHP填充了該值。之後,它被硬編碼到HTML中並且不會改變。

如果refreshTime依賴於任何發生的事情對日的eServer,你必須讓服務器與動態生成的內容一起返回新的時間間隔:

var refreshTime = <?= $this->refreshTime ?>; // set initial value 

function refreshContent() { 
    $.ajax({ 
     url: '/url/that/generates/no/longer/plain/html', 
     dataType: 'json', 
     success: function(data) { 
      refreshTime = data.refreshTime; 
      $('#dynamicContent').html(data.content); 
      setTimeout(refreshContent, refreshTime); 
     } 
    }); 
} 
refreshContent(); 

然後你就必須修改生成URL的腳本返回包含新內容的json數據結構以及新的刷新時間。