nodejs 任务调度使用
使用的模块
node-schedule的使用
例子:
1:确定时间
var schedule = require("node-schedule");
console.log("执行任务开始");
var date = new Date(2015,4,9,23,44,0);
var j = schedule.scheduleJob(date, function(){
console.log("执行任务");
});
2:每小时的固定时间
var rule = new schedule.RecurrenceRule();
rule.minute = 40;
console.log("执行任务开始");
var j = schedule.scheduleJob(rule, function(){
console.log("执行任务");
});
3:一个星期中的某些天的某个时刻执行
var rule = new schedule.RecurrenceRule();
console.log("执行任务开始");
rule.dayOfWeek = [0, new schedule.Range(1, 6)];
rule.hour = 20;
rule.minute = 0;
var j = schedule.scheduleJob(rule, function(){
console.log("执行任务");
});
4:每秒执行
var rule = new schedule.RecurrenceRule();
var times = [];
for(var i=1; i<60; i++){
times.push(i);
}
rule.second = times;
var c=0;
var j = schedule.scheduleJob(rule, function(){
c++;
console.log(c);
});