在Node.js中每4小时安排一次任务

Sye*_*idi 2 node.js

如何使用Node.js中的"node-schedule"在4小时后安排任务运行目前我的代码如下所示,但它没有按预期响应.

var schedule = require('node-schedule');
var task = schedule.scheduleJob('* */4 * * *', function () {
    console.log('Scheduled Task');
});
Run Code Online (Sandbox Code Playgroud)

Col*_*uch 9

您的语法创建了一个每4小时运行一次的cron.

您正在寻找的语法是0 */4 * * *.每4个小时执行一次.

您可以使用网站http://crontab.guru测试cron语法

在节点中设置cron的另一个选项是使用规则.请参阅https://github.com/node-schedule/node-schedule

var cron = require('node-schedule');
var rule = new cron.RecurrenceRule();
rule.hour = 4;
rule.minute = 0;
cron.scheduleJob(rule, function(){
    console.log(new Date(), 'Every 4 hours');
});
Run Code Online (Sandbox Code Playgroud)