微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 如何在一定时间内自动重新加载网页?

我有一个网站,我想在一定时间重新加载,如下午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

原文地址:https://www.jb51.cc/js/152104.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐