問題描述
我正在開發一個有兩個背靠背網絡廣播的網站.我使用 PHP 來顯示應該啟用和禁用按鈕的時間.現在我需要使用Javascript在第一次廣播之前,第二次廣播之前和第二次廣播之后自動刷新頁面.我實現了以下腳本,它確實在給定時間刷新了頁面,但是,它并不能完全按照我的需要工作.我需要更改腳本,使其僅在美國東部標準時間周日晚上 7:45、晚上 8 點和晚上 8:30 刷新頁面.
I am developing a website that has two back to back web broadcasts. I have used PHP to display the time that the buttons should be enabled and disabled. Now I need to use Javascript to automatically refresh the page before the first broadcast, before the second broadcast and after the second broadcast. I implemented the following script and it does refresh the page at the given time, however, it doesn't work exactly as I need it to. I need to alter the script so that it refreshes the page on Sundays at 7:45pm, 8pm and 8:30pm EST only.
我正在使用修改后的腳本 從這個回答的問題
I'm using a modified script 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() 函數.
Then I call the refreshAt() function.
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 實現刷新.
Where I get confused is how to alter this script to only implement the refresh for EST on Sundays.
推薦答案
我想通了.這是腳本:
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() 函數:
And then I just call the refreshAt() function:
refreshAt(20,00,0,1); //Will refresh the page at 8:00pm on Monday UTC or 3:00pm EST
這篇關于如何在特定時區的特定時間刷新特定日期的頁面?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!