我正在开发一个有两个背靠背网络广播的网站。我已经使用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刷新。
答案 0 :(得分: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
答案 1 :(得分:1)
您甚至可以在一周中添加测试,例如:
now.getDay() == "Sunday"
答案 2 :(得分:1)
另一种方法是使用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控制面板页面上手动发出推送事件,并且将重新加载当前正在查看该页面的所有人。
答案 3 :(得分:1)
function refreshAt(day, hours, minutes, seconds)
{
Date.getDay() = day
...
}
0是星期日,6是星期六。