nodejs定时任务node-schedule
1:使用npm安装node-schedule模块
npm
install
node-schedule
(1)每隔5分钟执行一次:
var schedule = require('node-schedule'); var rule = new schedule.RecurrenceRule(); rule.minute = [0,5,10,15,20,25,30,35,40,45,50,55]; var j = schedule.scheduleJob(rule,function(){ console.log("执行任务:"+new Date()); });
(2)上午8点到晚上20点每隔5分钟执行一次:
var schedule = require('node-schedule'); var rule = new schedule.RecurrenceRule(); rule.hour = [8,9,10,11,12,13,14,15,16,17,18,19,20]; rule.minute = [0,5,10,15,20,25,30,35,40,45,50,55]; var j = schedule.scheduleJob(rule,function(){ console.log("执行任务:"+new Date()); });
2:以下内容参考:
http://www.codexpedia.com/javascript/nodejs-cron-schedule-examples/
Using the node-schedule to schedule a job to run at a specific time on a specific date. As the first example, the node-schedule module is imported and save it in the variable cron. In the following examle, the require statement will be ommitted and this variable cron will be used.
1 2 3 4 5 6 |
|
Schedule a recurring job using the RecurrenceRule, example 1.
1 2 3 4 5 |
|
Schedule a recurring job using the RecurrenceRule, example 2.
1 2 3 4 5 6 7 8 |
|
Specify the schedule as an object literal.
1 2 3 4 |
|
Specify the schedule in unix cron syntax.
1 2 3 4 |
|