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>
4

1 回答 1

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>

我认为如何配置它很清楚,但是如果您有一些问题,请随时询问...

于 2012-10-08T19:51:08.877 回答