2012-01-08 60 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>