我需要每 30 分钟(整点和整点后 30 分钟)重新加载页面内容。我正在考虑使用 JavaScript,并且尝试了下面的代码,但它进入了无限循环。我不知道如何更改它以避免循环。
meta
标记不会有帮助,因为它们无法在某一分钟执行。
function refreshContent() {
var tDate = new Date();
thisHour = tDate.getHours();
thisMinute = tDate.getMinutes();
thisSecond = tDate.getSeconds();
setTimeout("refreshContent()",60000); // in milliseconds = 1 minute
if ( thisMinute == 0 || thisMinute == 30 ) {
location.reload();
}
}
refreshContent();
最佳答案
您只需要设置一个计时器并计算下一次刷新的时间。这样,当页面恰好在 :00 或 :30 加载时,您可以防止页面重复重新加载自身。
var minute = new Date().getMinutes(),
nextRefresh = (30 - (minute % 30)) * 60 * 1000;
setTimeout( function() { location.reload(); }, nextRefresh );
30 - (分钟 % 30)
计算距离下一个半小时还有多少分钟,* 60 * 1000
将其转换为毫秒。触发重新加载后,计时器设置为 30 分钟。
关于javascript - 每小时重新加载页面 :00 minute and the :30 minute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38128078/