【问题标题】:How to refresh page on specific day at specific time in specific time zone?如何在特定时区的特定时间刷新特定日期的页面?
【发布时间】:2012-02-05 12:31:46
【问题描述】:

我正在开发一个有两个背靠背网络广播的网站。我使用 PHP 来显示应该启用和禁用按钮的时间。现在我需要使用Javascript在第一次广播之前,第二次广播之前和第二次广播之后自动刷新页面。我实现了以下脚本,它确实在给定时间刷新了页面,但是,它并不能完全按照我的需要工作。我需要更改脚本,使其仅在美国东部标准时间周日晚上 7 点 45 分、晚上 8 点和晚上 8 点 30 分刷新页面。

我正在使用修改后的脚本from this answered question

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

if(now.getUTCHours() > hours ||
   (now.getUTCHours() == hours && now.getUTCMinutes() > minutes) ||
    now.getUTCHours() == hours && now.getUTCMinutes() == minutes && now.getUTCSeconds() >= seconds) {
    then.setUTCDate(now.getUTCDate() + 1);
}
then.setUTCHours(hours);
then.setUTCMinutes(minutes);
then.setUTCSeconds(seconds);

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

然后我调用 refreshAt() 函数。

refreshAt(19,45,0); //Will refresh the page at 7:45pm
refreshAt(20,00,0); //Will refresh the page at 8:00pm
refreshAt(20,30,0); //Will refresh the page at 8:30pm

我感到困惑的是如何更改此脚本以仅在周日为 EST 实现刷新。

【问题讨论】:

    标签: javascript date timezone refresh


    【解决方案1】:

    您甚至可以在一周中的某一天添加​​测试,例如:

    now.getDay() == "Sunday"
    

    【讨论】:

      【解决方案2】:

      另一种方法是使用Pusher。这保持与接收事件的开放连接。

      包含 Pusher javascript:

      <script src="http://js.pusher.com/1.11/pusher.min.js"></script>
      

      使用此代码绑定到“刷新”的推送事件:

      var pusher = new Pusher('abc123'); // Get a key when you sign up and replace this
      var refreshChannel = pusher.subscribe('refreshing');
      refreshChannel.bind('refresh', function(thing) {
        location.reload(true);
      });
      

      然后在晚上 8 点(或您想要的任何时间)在 Pusher 控制面板页面上手动发出 pusher 事件,所有当前查看该页面的人都将被重新加载。

      【讨论】:

      • 这实际上对我将来构建的东西非常有用。感谢分享!
      【解决方案3】:
      function refreshAt(day, hours, minutes, seconds) 
      {
          Date.getDay() = day
      ...
      }
      

      0 是星期日,6 是星期六。

      【讨论】:

      • 很抱歉这个答案的质量较低。如果你愿意,我会稍微编辑一下。我正在通过电话接听,所以质量不是一流的。
      【解决方案4】:

      我想通了。这是脚本:

      function refreshAt(hours, minutes, seconds, day) {
          var now = new Date();
          var then = new Date();
          var dayUTC = new Date();
      
          if(dayUTC.getUTCDay() == day) {
      
              if(now.getUTCHours() > hours ||
             (now.getUTCHours() == hours && now.getUTCMinutes() > minutes) ||
              now.getUTCHours() == hours && now.getUTCMinutes() == minutes && now.getUTCSeconds() >= seconds) {
              then.setUTCDate(now.getUTCDate() + 1);
              }
      
          then.setUTCHours(hours);
          then.setUTCMinutes(minutes);
          then.setUTCSeconds(seconds);
      
      
          var timeout = (then.getTime() - now.getTime());
          setTimeout(function() { window.location.reload(true); }, timeout);
          }
      }
      

      然后我只调用 refreshAt() 函数:

       refreshAt(20,00,0,1); //Will refresh the page at 8:00pm on Monday UTC or 3:00pm EST
      

      【讨论】:

      • 嗨@stacigh,如果没有 day 变量,这会是什么样子,我自己尝试做,但 javascript 不是我的事。我的目标是每天刷新某个 GMT 时间。谢谢!
      【解决方案5】:

      我和你有同样的问题。我正在构建一个听起来很像您的网站,还使用 ​​PHP 在广播时间之前和之后启用和禁用页面元素。您的解决方案似乎很有希望,但最终我对使用纯 Javascript 重新加载页面感到不满意。 Javascript 从客户端机器获取时间,而 PHP 从服务器获取时间,即使两者之间的微小差异也可能破坏整个系统。 (即页面可能会在 PHP 启用按钮前 30 秒刷新,导致一些查看者认为整个页面都已关闭。)

      我通过使用 PHP 告诉 Javascript 函数现在是什么时间解决了这个问题,它就像一个魅力。唯一的问题是它每天运行,而不是一周中的某一天,但这并不困扰我——除了广播日之外,用户没有理由查看该页面。

      这就是你所需要的——我把它放在&lt;body&gt; 标签之后:

      <script type="text/javascript">
      setTimeout(function() { window.location.reload(true); }, 1000*<?php 
      $then = mktime(15,00,0);
      $tomorrow = mktime(15, 00, 0, date("m") , date("d")+1);
      $now = time(); 
      if ($now > $then) {echo $tomorrow - $now;}
      else {echo $then - $now;}?>); </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-22
        • 1970-01-01
        • 1970-01-01
        • 2016-03-27
        • 1970-01-01
        • 2011-05-03
        • 1970-01-01
        • 2020-06-27
        相关资源
        最近更新 更多