node-schedule定时任务偶尔会提前一分钟,是我哪里写错了吗
发布于 6 年前 作者 dislido 3777 次浏览 来自 问答
const schedule = require('node-schedule');

const jobMap = new Map();

function send(msg) {
  const d = new Date();
  console.log(`[info]${d.toLocaleDateString()} ${d.toLocaleTimeString()} send:${msg}`);
}

jobMap.set('整点报时', schedule.scheduleJob('1 0 * * * *', () => {
  send((new Date()).getHours());
}));

输出的log

[info]2017-11-7 20:00:01 send:20
[info]2017-11-7 20:58:57 send:20
[info]2017-11-7 22:00:01 send:22
[info]2017-11-7 23:00:01 send:23
[info]2017-11-8 00:00:01 send:0
[info]2017-11-8 00:58:58 send:0
[info]2017-11-8 02:00:01 send:2
[info]2017-11-8 03:00:01 send:3
[info]2017-11-8 04:00:01 send:4
[info]2017-11-8 04:58:57 send:4
[info]2017-11-8 06:00:01 send:6
[info]2017-11-8 07:00:01 send:7
[info]2017-11-8 08:00:01 send:8
[info]2017-11-8 08:58:58 send:8
[info]2017-11-8 10:00:01 send:10
[info]2017-11-8 11:00:01 send:11

每隔3小时会有一次提前一分钟,是哪里出了问题? node 8.6.0 windows server 2012 x64 node-schedule 1.2.5

顺便说一下,现在我是这样解决的

let hour = (new Date()).getHours();
// ...
schedule.scheduleJob('1 0 * * * *', () => {
  send((++hour) % 24);
})
1 回复

这样也可以 const rule = new schedule.RecurrenceRule(); rule.minute=0;

回到顶部