我有一个每隔一分钟运行一次的javascript函数.
setInterval(function(){
//first, check time, if it is 9 AM, reload the page
var now = new Date();
if (now.getHours() == 9 {
window.refresh();
}
//do other stuff
},60000);
Run Code Online (Sandbox Code Playgroud)
现在的问题是,我希望重新加载每天只发生一次.因为该函数每分钟运行一次,所以下次启动时,如果它在9AM到10AM之间,它将再次重新加载页面.如何仅重新加载一次?
我可以通过创建另一个间隔函数来实现它,该函数每小时触发一次并检查是否应该重新加载.但由于我已经拥有每分钟运行的上述功能,我可以从那里开始吗?
如果我最终创建另一个每小时检查一次的功能.如果这2个功能在同一时间启动会发生什么?
我会存储上次刷新的日期,计算差异,并且它不到6小时(为了安全起见),您不需要刷新.
var lastRefresh = new Date(); // If the user just loaded the page you don't want to refresh either
setInterval(function(){
//first, check time, if it is 9 AM, reload the page
var now = new Date();
if (now.getHours() == 9 && new Date() - lastRefresh > 1000 * 60 * 60 * 6) { // If it is between 9 and ten AND the last refresh was longer ago than 6 hours refresh the page.
location.reload();
}
//do other stuff
},60000);
Run Code Online (Sandbox Code Playgroud)
我希望这就是你的意思.请注意,这是未经测试的.
| 归档时间: |
|
| 查看次数: |
3149 次 |
| 最近记录: |