3 回答

TA貢獻1865條經驗 獲得超7個贊
以下JavaScript代碼段可讓您在給定的時間刷新:
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()函數。
refreshAt(15,35,0); //Will refresh the page at 3:35pm

TA貢獻1772條經驗 獲得超8個贊
<META HTTP-EQUIV="Refresh" CONTENT="5">
這將迫使頁面每5秒重新加載一次。只需計算正確的時間間隔并將其添加到內容標簽即可

TA貢獻2051條經驗 獲得超10個贊
我發現此頁面有類似的問題,并用它找出了可能對某些人有用的更具體的答案。對于此項目,我們希望確保一旦發生全球關注的現場活動時頁面就會刷新,從而激活嵌入在用戶頁面上的播放器(我知道狹窄的用例-其他人可能會更好地用于它)。
上述答案中的一個挑戰是如何處理時區轉換,這對我們來說是一個更大的問題,因為我們要確保頁面在特定的日期和時間刷新。為此,我獲取了目標日期和今天的日期的UTC版本,將其轉換為GMT,然后將Andrew的超時功能設置為兩者之間的差值。
var target = new Date("January 28, 2011 13:25:00");
timeOffset = target.getTimezoneOffset() * 60000;
targetTime = target.getTime();
targetUTC = targetTime + timeOffset;
var today = new Date();
todayTime = today.getTime();
todayUTC = todayTime + timeOffset;
refreshTime = (targetUTC - todayUTC);
if (refreshTime > 1) {
setTimeout(function() { window.location.reload(true); }, refreshTime);
}
添加回答
舉報