使用时区强制在特定时间刷新页面

时间:2012-10-08 17:27:49

标签: javascript timezone refresh

我正在尝试强制页面在特定时间用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 个答案:

答案 0 :(得分: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>

我认为很清楚如何配置它但是如果你有一些问题可以随意问...