我需要一个高度可靠的定时器/时钟来监控时间,并且每5分钟触发一次(实际上是5分-1秒,例如xx:59)。它需要是异步的,因为5分钟标记处的触发器将调用将在后台执行任务的一个或多个函数。
所以...我想出了这个超级简单的例程。我正在寻找反馈。我应该做些什么吗?这是一个可靠的解决方案,可用于生产吗?建议和批评是值得欢迎的。好,坏,或无动于衷!
**已编辑:忘记这行代码:
minutes = ( mminutes < 10 ? "0" : "" ) + mminutes;
var minuteData,hours,minutes,seconds;
setInterval(function() {
var date = new Date;
hours = date.getHours();
mminutes = date.getMinutes();
seconds = date.getSeconds();
minutes = ( mminutes < 10 ? "0" : "" ) + mminutes;
//get the second digit of minutes
minuteData = minutes.toString()[1];
console.log(minuteData);
//test if at time x4:59 or x9:59, hence 1 sec before the 5min interval
var trigger = ((seconds == 59 && (minuteData == "9" || minuteData == "4")) ? true : false);
console.log(trigger);
//trigger is just a proxy for a task that will be called
}, 1000);
答案 0 :(得分:1)
我可能会将该函数定义为命名函数,然后在setInterval上调用该函数。可能性能相同,但允许您在页面加载或特定操作等上调用它。
function testTime(){
var minuteData,hours,minutes,seconds;
var date = new Date;
hours = date.getHours();
minutes = date.getMinutes();
seconds = date.getSeconds();
//get the second digit of minutes
minuteData = minutes.toString()[1];
console.log(minuteData);
//test if at time x4:59 or x9:59, hence 1 sec before the 5min interval
var trigger = ((seconds == 59 && (minuteData == "9" || minuteData == "4")) ? true : false);
console.log(trigger);
}
setInterval(function() {testTime()}, 1000);