2012-01-08 96 views
4

我正在开发一个有两个背对背网络广播的网站。我已经使用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的刷新。

回答

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 
+0

hi @stacigh,没有日变量,这会是什么样子,试图做自己,但JavaScript不是我的东西。我的目标是每天更新GMT时间。谢谢! – Jaypee 2015-05-14 22:42:41

1

您可以添加测试,即使是星期几,例如:

now.getDay() == "Sunday" 
1

另一种方法是使用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控制面板页面上,并且当前正在查看该页面的所有人都将被重新加载。

+0

这实际上是东西我建的未来非常有用。感谢分享! – stacigh 2012-01-08 19:53:04

1
function refreshAt(day, hours, minutes, seconds) 
{ 
    Date.getDay() = day 
... 
} 

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

+0

抱歉这个答案质量较差。如果你喜欢,我会稍微编辑它。我正在接听电话,所以质量不是一流的。 – Bry6n 2012-01-08 19:45:45

1

我有同样的问题,因为你。我正在构建一个类似于您的网站,同时还使用PHP在广播时间之前和之后启用和禁用页面元素。您的解决方案看起来很有希望,但最终我不满意使用纯Javascript来进行页面重新加载。 Javascript从客户端的机器中获得时间,而PHP从服务器获取它的时间,即使两者之间的小差异也会破坏整个系统。 (也就是说,在PHP启用按钮之前,页面可能会刷新30秒,导致一些观众认为整个事情都停止了。)

我通过使用PHP告诉Javascript函数是什么时间解决了这个问题,奇迹般有效。唯一的问题是它每天都在运行,而不是每周的一天,但这并不影响我 - 用户将没有理由查看除广播日以外的页面。

这是所有你需要 - 我把这个权利<body>标签后:

<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>