如何在特定时间自动重新加载网页?

发布于 2024-08-01 12:46:04 字数 71 浏览 5 评论 0原文

我有一个网站,我希望在特定时间(例如下午 3:35)重新加载,而不是在特定时间间隔(例如 5 分钟)之后重新加载。 我怎么做?

I have a website that I want to be reloaded at a certain time, like 3:35pm, not after a specific interval like 5min. How do I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

梦明 2024-08-08 12:46:04

基本上,有许多 JavaScript 代码可以每隔几分钟刷新页面或其他内容,您也可以编辑它们以在几小时内刷新。 像这样:(

//enter refresh time in "minutes:seconds" Minutes: 0 to Whatever
//Seconds should range from 0 to 59
var limit = "0:30";

if (document.images) {
    var parselimit = limit.split(":");
    parselimit = parselimit[0] * 60 + parselimit[1] * 1;
}
var beginrefresh = function () {
    if (!document.images) return if (parselimit == 1) window.location.reload()
    else {
        parselimit -= 1;
        curmin = Math.floor(parselimit / 60);
        cursec = parselimit % 60;
        if (curmin != 0) curtime = curmin + " minutes and " + cursec + " seconds left until page refresh!";
        else curtime = cursec + " seconds left until page refresh!";
        window.status = curtime;
        setTimeout("beginrefresh()", 1000);
    }
}

window.onload = beginrefresh;

现在只需计算您希望它刷新的分钟和秒数,例如每天中午,如果现在是中午:

var limit = "1440:00";

现在您可以使用此代码,除了,它们中的大多数不适用于服务器时间,并且与根据您向我们提供的信息,我们确实无法做更多的事情,请编辑您的问题并告诉我们您是否希望它与服务器时间或其他时间同步。

Basically, there are many javascript codes out there that can refresh the page ever so minutes or something, you can edit them to refresh at hours too. Like this one:

//enter refresh time in "minutes:seconds" Minutes: 0 to Whatever
//Seconds should range from 0 to 59
var limit = "0:30";

if (document.images) {
    var parselimit = limit.split(":");
    parselimit = parselimit[0] * 60 + parselimit[1] * 1;
}
var beginrefresh = function () {
    if (!document.images) return if (parselimit == 1) window.location.reload()
    else {
        parselimit -= 1;
        curmin = Math.floor(parselimit / 60);
        cursec = parselimit % 60;
        if (curmin != 0) curtime = curmin + " minutes and " + cursec + " seconds left until page refresh!";
        else curtime = cursec + " seconds left until page refresh!";
        window.status = curtime;
        setTimeout("beginrefresh()", 1000);
    }
}

window.onload = beginrefresh;

(now just calculate the minutes and seconds you want it to refresh, like for example noon everyday if it were noon now:

var limit = "1440:00";

Now you could use this code except, most of them don't work with server time, And with the information you provided us, we really can't do anything more. Edit your question and tell us if you want it to be timed with the servers time, or something else.

伏妖词 2024-08-08 12:46:04

以下 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

请注意,此代码将根据客户端本地时间刷新。 如果您希望它在特定时间而不考虑客户端的时区,您可以替换 get*()set*()getTime() 除外))在时间对象上使用其 getUTC*()setUTC*() 等效项,以便将其固定到 UTC。

The following JavaScript snippet will allow you to refresh at a given time:

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);
}

Then you can add a script tag to call the refreshAt() function.

refreshAt(15,35,0); //Will refresh the page at 3:35pm

Note that this code will refresh based on the client local time. If you want it to be at a specific time regardless of the client's timezone, you can replace get*() and set*() (except getTime()) on the time objects with their getUTC*() and setUTC*() equivalent in order to pin it to UTC.

如果没有你 2024-08-08 12:46:04

我发现这个页面有一个类似的问题,并用它来破解一个可能对某些人有用的更具体的答案。 对于这个项目,我们希望确保一旦全球感兴趣的现场活动即将进行,页面就会刷新,从而激活用户页面上的播放器嵌入(我知道,狭窄的用例 - 其他人可能有更好的用途)它)。

上述答案中的一个挑战是如何处理时区转换,这对我们来说更是一个问题,因为我们希望确保页面在特定的日期和时间刷新。 为此,我获取了目标日期和今天日期的 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);
}

I found this page with a similar question and used it to hack out a more specific answer that may be of use to some. For this project, we wanted to make sure that the page refreshed once a live event of global interest was about to go on, activating the player embed on the user's page (narrow use case, I know -- others might have a better use for it).

One challenge in the above answers was how to deal with time zone conversions, which was more of an issue for us because we wanted to make sure that the page refreshed at a specific day and time. To do this, I grabbed a UTC version of the target date and today's date, converted them to GMT, then set Andrew's timeout function to the difference between the two.


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);
}
峩卟喜欢 2024-08-08 12:46:04
<META HTTP-EQUIV="Refresh" CONTENT="5">

这将强制页面每 5 秒重新加载一次。 只需计算正确的间隔并将其添加到内容标签即可

<META HTTP-EQUIV="Refresh" CONTENT="5">

this will force page to reload every 5 seconds. Just calculate the correct interval and add it to content tag

呆萌少年 2024-08-08 12:46:04

基本上,当访问页面时,计算访问时间和要重新加载页面的时间之间剩余多少时间,并在元刷新标头中使用该剩余时间。 显然,这需要在 CGI 脚本或 Web 应用程序中完成,或者可能使用 SSI(服务器端包含)来完成; 如果您只有静态 HTML 文件,它将无法工作。

另一种选择是使用 Javascript,但如果客户端禁用了 Javascript,它将无法工作。

Basically, when the page is accessed, calculate how much time is remaining between the access time and the time you want to reload the page, and use that remaining time in the meta refresh header. Obviously this would need to be done in a CGI script or web application, or possibly with SSI (server-side includes); it won't work if all you have is a static HTML file.

Another alternative would be to use Javascript, but it won't work if the client has Javascript disabled.

盛装女皇 2024-08-08 12:46:04

这更适合我的目的。

如果您能够使用 Jquery 和 MomentJs,您可以执行以下操作:

(function () {
    var $ = require('jquery');
    var moment = require('moment');

    function refreshPageAtTime(expiration, countdownElement) {
        var now = moment.utc();
        console.log('now', now);
        var expirationMoment = moment.utc(expiration, 'YYYY-MM-DD kk:mm:ss');
        console.log('target', expirationMoment);
        var secondsUntilRefresh = expirationMoment.diff(now, 'seconds');//http://momentjs.com/docs/#/displaying/difference/
        console.log('diff in seconds', secondsUntilRefresh);
        if (secondsUntilRefresh > 1) {
            setInterval(function () {
                secondsUntilRefresh--;
                console.log('seconds remaining', secondsUntilRefresh, 'seconds');
                if (secondsUntilRefresh <= 10) {
                    countdownElement.html(secondsUntilRefresh + '...');
                    if (secondsUntilRefresh === 0) {
                        console.warn('Refreshing now at ' + moment.utc());
                        window.location.reload(true);
                    }
                }
            }, 1000 * 1);
        }
    }

    $(document).ready(function () {
        var expiration = $('form').attr('data-expiration');
        console.log('expiration', expiration);
        $('.btn-primary:submit').after('<div id="countdownToRefresh" style="display: inline-block; color: #999; padding: 10px;"></div>');
        refreshPageAtTime(expiration, $('#countdownToRefresh'));

    });
})();

This worked better for my purposes.

If you're able to use Jquery and MomentJs, you can do this:

(function () {
    var $ = require('jquery');
    var moment = require('moment');

    function refreshPageAtTime(expiration, countdownElement) {
        var now = moment.utc();
        console.log('now', now);
        var expirationMoment = moment.utc(expiration, 'YYYY-MM-DD kk:mm:ss');
        console.log('target', expirationMoment);
        var secondsUntilRefresh = expirationMoment.diff(now, 'seconds');//http://momentjs.com/docs/#/displaying/difference/
        console.log('diff in seconds', secondsUntilRefresh);
        if (secondsUntilRefresh > 1) {
            setInterval(function () {
                secondsUntilRefresh--;
                console.log('seconds remaining', secondsUntilRefresh, 'seconds');
                if (secondsUntilRefresh <= 10) {
                    countdownElement.html(secondsUntilRefresh + '...');
                    if (secondsUntilRefresh === 0) {
                        console.warn('Refreshing now at ' + moment.utc());
                        window.location.reload(true);
                    }
                }
            }, 1000 * 1);
        }
    }

    $(document).ready(function () {
        var expiration = $('form').attr('data-expiration');
        console.log('expiration', expiration);
        $('.btn-primary:submit').after('<div id="countdownToRefresh" style="display: inline-block; color: #999; padding: 10px;"></div>');
        refreshPageAtTime(expiration, $('#countdownToRefresh'));

    });
})();
渡你暖光 2024-08-08 12:46:04

希望对您有帮助,您可以设置准确的刷新时间

var target = new Date("November 18, 2019 10:00: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);
}

I hope this help,you can set the exact time for refresh

var target = new Date("November 18, 2019 10:00: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);
}
一笑百媚生 2024-08-08 12:46:04

如果您使用 Flask,您可以将变量设置为与网络时间同步。 在 Flash 应用程序

from datetime import *`
    def syncRefresh():`
      while (datetime.now().second % 10 !=0):`
          continue`
       return True`

和 @app.route('/',methods =["GET"})

    def table():
       ....
         if syncRefresh():
            refreshNow = True  # refreshNow is the variable passed to the web page

和 html 页面中

     {% if refreshNow  %}
         <meta http-equiv="refresh"   content="1">
    {% endif %}

if you using Flask you can set variable synchronized to network time. In the flash app

from datetime import *`
    def syncRefresh():`
      while (datetime.now().second % 10 !=0):`
          continue`
       return True`

and @app.route('/', methods =["GET"})

    def table():
       ....
         if syncRefresh():
            refreshNow = True  # refreshNow is the variable passed to the web page

and in the html page

     {% if refreshNow  %}
         <meta http-equiv="refresh"   content="1">
    {% endif %}
弃爱 2024-08-08 12:46:04

在给定的分钟和秒刷新→即每小时固定时间可以如下:

<script type="text/javascript">
function refreshAt(minute, second) {
    var date= new Date();
    var hr = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    if(m ==  minute && s == second)
    {
        window.location.reload(true);
    }
    setTimeout(function() { refreshAt(minute, second); },600);
};
</script>

refresh at a given minute and second → i.e. every hour at fixed time can be as follows:

<script type="text/javascript">
function refreshAt(minute, second) {
    var date= new Date();
    var hr = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    if(m ==  minute && s == second)
    {
        window.location.reload(true);
    }
    setTimeout(function() { refreshAt(minute, second); },600);
};
</script>
夜清冷一曲。 2024-08-08 12:46:04

使用它每 20 秒刷新一次页面。

<META HTTP-EQUIV="refresh" CONTENT="20">

Use this to refresh the page every 20 seconds.

<META HTTP-EQUIV="refresh" CONTENT="20">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文