donljt2606 2011-08-24 20:21
浏览 96
已采纳

当刷新时间发生变化时,如何让jQuery Mobile动态刷新div?

Writing code in php and jQueryMobile.

I am trying to get a div to refresh based on a variable 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>

So this code will fire once and refresh the div dynamicContent after refreshTime has lapsed. The problem is that I want to keep refreshing with the new value of refreshTime that will be set once the refresh has happened.

I have scoured the web, looking for a jQuery Mobile way to do this, although if a jQuery way works I am happy to try that and see what that looks like on a mobile.

Note that the line:

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

Is only there for debugging

Any comments/thoughts/answers appreciated.

Cheers

  • 写回答

1条回答 默认 最新

  • dongsheng8664 2011-08-24 20:49
    关注

    You've hard coded the refresh time into the setTimeout function. It looks dynamic, but only because the value is set when the page is generated and PHP fills in the value. After that, it's hardcoded into the HTML and doesn't change.

    If the refreshTime depends on whatever's happening on th eserver, you'd have to get the server to return that new time interval along with the dynamically generated content:

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

    Then you'll have to modify the script generating the URL to return a json data structure which contains the new content, as well as the new refresh time.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!