2016-06-30 98 views
0

我需要每30分钟(在小时过后的一小时和30分钟)重新加载页面内容。我正在为此考虑JavaScript,并且我已经尝试了下面的代码,但它进入了一个无限循环。我不知道如何改变它以避免循环。每隔一小时在(00分钟)和30分钟内重新加载页面

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

回答

3

您只需设置一个计时器并计算下次刷新的时间。这样,当页面加载到:00或30时,可以防止页面重复加载。

var minute = new Date().getMinutes(), 
    nextRefresh = (30 - (minute % 30)) * 60 * 1000; 

setTimeout(function() { location.reload(); }, nextRefresh); 

30 - (minute % 30)计算多少分钟,直到下一个半小时* 60 * 1000转换到这毫秒。重新启动后,计时器设置为30分钟。

+0

更容易!我们可能会每15分钟做一次更新(00,15,30,45),所以要做到这一点:nextRefresh =(15 - (分钟%15))* 60 * 1000;'? – Pat

+1

是的,没错。 – JJJ

+0

感谢您的帮助 – Pat

-2

您会立即调用refreshContent功能,当你设置setTimeout。尝试setTimeout(refreshContent, 60000);