2012-10-08 76 views
1

我試圖強制頁面在特定時間用js刷新後,挖掘周圍我發現下面的腳本。但是,它似乎沒有考慮時區。我將如何實現?在特定時間強制頁面刷新時區

<html> 
<head> 
<title></title> 
<script type="text/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(15,06,0); //Will refresh the page at 3:06pm 
</script> 
</head> 
<body onLoad="setInterval('getCurrentTime()', 1000);"> 
<div id="time"></div> 
content 
</body> 
</html> 
+1

要清楚,您希望該頁面在某個特定時間根據服務器時間刷新?這樣就可以在每個客戶的同時更新它? – Develoger

+0

我將手動重命名一個html文件,所以當他們刷新頁面時,它會刷新到新頁面。這是一次性事件。我不確定它是如何得到時間的,我假設它來自本地機器,所以我擔心觀衆不會同時刷新所有內容。 –

+0

如果你要重命名文件重新加載同一頁面的重點是什麼? – Develoger

回答

0

在這裏,你走了,重裝爲每一位用戶完全按照自己的全局變量在腳本中定義將會發生:

編輯!我在代碼,所以我已經更新了腳本錯誤...

編輯工作示例:
http://simplestudio.rs/yard/tinediffredir/content.html

<html> 
    <head> 
     <title>Chk diff and reload if match</title> 

     <script> 

      var reload_time = "15060"; // this is 15:06:00 - change to desired reload time 
      var reload_hour_diff = 15; // for cheking diff - change to desired hours 
      var reload_minute_diff = 6; // for cheking diff - change to desired minutes 
      var time_zone_offset = "-5"; // globally looking from GMT time, change this according to DST 

      var reload_time_checker = setInterval(function(){reload_page()},300); 
      var diff = null; 

      function chk_reload_moment(offset) { 

       dat = new Date(); 

       utc = dat.getTime() + (dat.getTimezoneOffset() * 60000); 

       default_date = new Date(utc + (3600000*offset)); 

       var default_year = default_date.getFullYear(); 
       var default_month = default_date.getMonth(); 
       var default_day = default_date.getDate(); 
       var default_hour = default_date.getHours(); 
       var default_minutes = default_date.getMinutes(); 
       var default_seconds = default_date.getSeconds(); 

       user_date = new Date(); 

       var user_year = user_date.getFullYear(); 
       var user_month = user_date.getMonth(); 
       var user_day = user_date.getDate(); 
       var user_hour = user_date.getHours(); 
       var user_minutes = user_date.getMinutes(); 
       var user_seconds = user_date.getSeconds(); 

       user_current = user_hour+""+user_minutes+""+user_seconds; 

       default_current_f = default_day+"/"+default_month+"/"+default_year+" "+default_hour+":"+default_minutes+":"+default_seconds; 
       user_current_f = user_day+"/"+user_month+"/"+user_year+" "+user_hour+":"+user_minutes+":"+user_seconds; 

       var timeEnd = new Date(user_current_f); 
       var timeEndH = timeEnd.getHours(); 
       var timeEndM = timeEnd.getMinutes(); 

       var new_reload_minute_diff = 60+reload_minute_diff; 
       diff = (timeEndH - reload_hour_diff + 12) + " hours " + (new_reload_minute_diff - timeEndM) + " minutes";  

       if (user_current == reload_time) { 

        return true; 

       } 
       else { 

        return false; 

       } 

      } 

      function reload_page() { 

       var chktime = chk_reload_moment(time_zone_offset); 
       if (chktime) { 

        window.location.reload(); 

       } 
       else { 

        var timer_div = document.getElementById('timer'); 
        timer_div.innerHTML = "remaining: " + diff + " until new content"; 

       } 

      } 

     </script> 

    </head> 
    <body> 

     <div id="timer"> 

     </div> 

    </body> 
</html> 

我認爲這是明確如何配置它,但如果你有一些問題隨意問...