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

标签 javascript html reload

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

最佳答案

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

关于javascript - 如何在特定时间自动重新加载网页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1217929/

相关文章:

javascript - 使用自定义用户界面设备连接到 OpenSeadragon

html - 具有动态宽度的一行中的 Div(对齐)

javascript - 我如何使用 JavaScript 中的 RegExp 检查多个模式?

javascript - 页面重新加载和图像缓存

javascript - fetch(),你如何发出非缓存请求?

javascript - 按时间对 JSON 数组进行排序,以 12 小时格式显示

javascript - 为什么这个 jquery 函数在 .animate() 之后不重复?

javascript - onload 事件在使用 JavaScript 的 body 标记中不起作用

javascript - 在 Router5 中刷新 View 状态时,另一个 View 的一部分重新出现

javascript - 重新加载 div 的最佳方法是什么?