我有一个问题,我想检查时间并根据它做一些事情。例如,从午夜12点到凌晨1点,我想阻止页面刷新。我该如何做到这一点?
function RefreshPage()
{
if (refresh == 'Y')
{
CheckAndStopAutoRefresh();
}
}
function CheckAndStopAutoRefresh()
{
var d = new Date();
var currentHour = d.getHours(); // 0-23
if (currentHour >= 0 && currentHour < 1)
{
return;
}
else
{
window.location=window.location; // refresh
}
}
答案 0 :(得分:5)
//initialize date object
var d = new Date();
var currentHour = d.getHours(); //note 0-23
//you could use currentHour == 0 but this expression allows for a range
if (currentHour >= 0 && currentHour < 1)
{
console.log('between 0:00 and 1:00 hrs');
}
else { console.log('after 1am'); }
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date提供日期对象的概述
我不太清楚'停止刷新'是什么意思我不相信可以禁用刷新/ F5,但也许如果你修改你的问题或指明你的意图我可以再次尝试这个问题。
EDITED
- 编辑以反映更新的问题。
我会使用setInterval(code,millisec,lang)
function CheckAndStopAutoRefresh() {
//initialize date object
var d = new Date();
var currentHour = d.getHours(); //note 0-23
//you could use currentHour == 0 but this expression allows for a range
if (currentHour >= 0 && currentHour < 1)
{
console.log('between 0:00 and 1:00 hrs');
}
else { console.log('after 1am'); }
}
setInterval(CheckAndStopAutoRefresh,60000); //60,000 milliseconds in a minute