【问题标题】:Refresh Div at a certain time在特定时间刷新 Div
【发布时间】:2013-12-03 22:54:30
【问题描述】:

好的,所以我正在创建一个广播播放器,基本上我需要 Title、Content div、Next show Div 在某些时间刷新,例如上午 9 点和下午 12 点。我有 JQuery 代码可以在某个时间刷新页面,但这并不是我所追求的。有什么想法吗?

代码:

function refreshAt(hours, minutes, seconds) {
var now = new Date();
var then = new Date();

if(now.getHours() > hours ||
   (now.getHours() == hours && now.getMinutes() > minutes) ||
    now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >= seconds) {
    then.setDate(now.getDate() + 1);
}
then.setHours(hours);
then.setMinutes(minutes);
then.setSeconds(seconds);

var timeout = (then.getTime() - now.getTime());
setTimeout(function() { window.location.reload(true); }, timeout);
}

然后我只需通过在我的页面上插入以下内容来调用 refreshAt 函数

<script type="text/javascript">refreshAt(04,30,0);</script> //page refreshes at 4:30am.

所以这会刷新整个页面。我只需要刷新标题和 2 个 div。 我需要在代码中添加/更改什么。

【问题讨论】:

  • 你刷新 div 背后的主要逻辑是什么你试图刷新以在 div 中加载一些数据?
  • 你好@Veerendra,它背后的逻辑是说就像一个演示者正在播出例如“约翰史密斯 - 09:00”,当下一位主持人在 12:00 播出时,我希望 div更改为“Peter Smith - 12:00”,听众无需刷新。

标签: php jquery html refresh


【解决方案1】:

这将在间隔过去后调用您当前的 URL 并重置您的 title 和您的 div 内容。

你必须这样写:

function refreshAt(hours, minutes, seconds) {
   var now = new Date();
   var then = new Date();

   if(now.getHours() > hours || (now.getHours() == hours && now.getMinutes() > minutes) || now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >=  seconds) {
           then.setDate(now.getDate() + 1);
   }
   then.setHours(hours);
   then.setMinutes(minutes);
   then.setSeconds(seconds);

   var timeout = (then.getTime() - now.getTime());
   setTimeout(function() {
      $.get( window.location.pathname, function( data ) {
             $('title') = $(data).filter('title').text();
             $('.DIV_CLASS') = $(data).filter('.DIV_CLASS').html();
             alert( "Load was performed." );
     });
   }, timeout);
}

注意:此脚本使用 jQuery,因此请包含最新的 jQuery 库。

【讨论】:

  • 谢谢你的回答,我会试试这个。
  • 是否可以根据现在的时间使用列表中的信息刷新 div?所以说就像我有一个人的名单,每个人都有时间他们正在播出。我需要脚本根据现在的时间选择一个人并将其显示在 div 中。你懂我的意思吗?有可能吗?
  • @user3011524 你的意思是获取用户的系统时间并根据它工作。
  • 是的,这正是我的意思:)
  • @Julian 请回答这个问题:href=%27http://stackoverflow.com/a/863624/631652
【解决方案2】:

你可以试试这个:

setTimeout(function() { 
    document.title = "new title";
    $(".divname").text("new div text");
}, timeout);

【讨论】:

  • 感谢您的回答,这与我所追求的类似,但我希望在某个时间刷新,例如下午 12:00,然后在下午 15:0 再次刷新。脚本必须能够从侦听器计算机获取时间,以便准确知道何时刷新。
【解决方案3】:

请尝试以下代码:

setTimeout(function(){
        $('#divid').load("pageurl #divid" >*",function(){

        });
  }, 6000);

您的 div 会在 6 秒后重新加载。

【讨论】:

  • 感谢您的回答,但这不是我想要的。
【解决方案4】:

你可以看看jQuery的load方法。

使用 is 可以加载 div 的内容。

    setTimeout(function() { 
      $("#youfirstdivid").load("url1"); 
       $("#youseconddivid").load("url2");

      $('title').text("new title"); 
   }, timeout);

别忘了把你的代码包装在里面

$(function(){
  //you code goes here

});

如果你想以某个固定的时间间隔刷新它,那么你可以使用 setInterval 而不是 setTimeout

【讨论】:

  • 感谢您的回答,但这并不是我所追求的。我需要在特定时间刷新这些 div,例如下午 4 点然后 8 点。
猜你喜欢
  • 1970-01-01
  • 2011-02-02
  • 2021-07-22
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-30
相关资源
最近更新 更多