繁体   English   中英

我怎样才能让页面在一天中的特定时间刷新?

[英]How can i make a page refresh in a certain time all days?

我希望我的页面每天 00:01 自动刷新。

我有这个

 var targetTime = new Date(); var now = targetTime.getTime(); targetTime.setHours(0, 17, 0, 0); // hour, minute, second, millisecond var time = targetTime.getTime() - now; window.setTimeout(function() { window.location.reload(true); }, time);

问题是当它是 00:01 时,网站总是重新刷新。

每分钟检查一次一天中的小时和分钟可能更有效,如下所示:

setInterval(() => {
  const date = new Date()
  const hour = date.getHours()
  const minute = date.getMinutes()
  if (hour === 0 && minute === 1) window.location.reload()
}, 60000)

好的,所以我已经设法做到了,而且有效!

function refreshAt(hours, minutes, seconds)
{
    var now = new Date(), then = new Date();
    then.setHours(hours,minutes,seconds,0);
    if(then.getTime()<now.getTime())
    {
        then.setDate(now.getDate() + 1);
    }

    var timeout = (then.getTime() - now.getTime());
    setTimeout(function() { window.location.reload(true); }, timeout);
}

refreshAt(0,1,0);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM