最近在做一个需要定时执行的任务,通过查资料,看到了node-schedule,感觉用起来简单方便,欢迎共同探讨。
github地址:https://github.com/mattpat/node-schedule
安装
npm install node-schedule
示例
1.确定时间,例如:2014年11月20日,17:30
var schedule = require('node-schedule');
var date = new Date(2014,11,20,17,30,0);
var j = schedule.scheduleJob(date, function() {
console.log('welcome to my house!');
});
//取消预设计划
j.cancel();
2.每小时的固定分钟。例如:每小时的24分钟
var schedule = require('node-schedule');
var rule = new schedule.RecurrenceRule();
rule.minute = 24;
var j = schedule.scheduleJob(rule, function() {
console.log('everybody up.');
});
3.一个星期中的某些天的某个时刻,例如:每周四,周五,周六,周天的17点
var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [0, new schedule.Range(4, 6)];
rule.hour = 17;
rule.minute =0;
var j = schedule.scheduleJob(rule, function() {
console.log('Happy birthday to you!');
});