
正文
Nodejs学习笔记(十二)--- 定时任务(node-schedule)
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
.wilson_body{color:#000;font-family:微软雅黑, PTSans, Arial, sans-serif;font-size:14px;font-style:normal;font-variant:normal;font-weight:normal;serif;line-height:110%;}
.wilson_body img{padding: 2px;border: 3px solid #F2F5F4;}
#menu ul{list-style:square;}
#menu li{ margin:10px auto 10px auto;}
.wilson_quote{ font-style:italic; color:#00CCFF; text-decoration:none;}
.wilson_body a{color:#00CCFF; text-decoration:none;}
.wilson_body a:hover{color:#00CCFF; text-decoration:underline;background-color: #8A8888;}
.wilson_highlight{color:black;font-family:黑体;color: #00ccff; font-size:16px; font-weight:lighter;}
.wilson_h1{ background: #8A8888; !important; border-radius: 6px 6px 6px 6px !important;box-shadow: 0 0 0 1px #e0e0e0, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #ffffff; font-family:微软雅黑, 宋体, 黑体, Arial;font-size:18px;font-weight:bold;height:25px;line-height:25px;margin:16px 0 !important;padding:5px 0 5px 20px;text-shadow:#222222 2px 2px 3px;}
.wilson_tip{color:#F00;font-family:黑体;font-style:italic; font-weight:500; }
.wilson_body table{padding: 0; margin-left:20px;}
.wilson_body table th { font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif; color: #4f6b72; border-right: 1px solid #C1DAD7; border-bottom: 1px solid #C1DAD7;
border-top: 1px solid #C1DAD7; letter-spacing: 2px; text-transform: uppercase; text-align: left; padding: 6px 6px 6px 14px; background: #CAE8EA no-repeat;
}
.wilson_body table th.nobg { border-top: 0; border-left: 0; border-right: 1px solid #C1DAD7; background: none; }
.wilson_body table td { border-right: 1px solid #C1DAD7; border-bottom: 1px solid #C1DAD7; background: #fff; font-size:14px; padding: 6px 6px 6px 14px; color: #4f6b72; }
.wilson_dl_img{width:30px;height:30px;}
.wilson_dl_text{float:left;}
目录
-
写在之前
-
Cron风格定时器
-
通配符解释
-
范围触发
-
递归规则定时器
-
对象文本语法定时器
-
取消定时器
-
写在之后
写在之前
- 写在之前
-
Cron风格定时器
- 通配符解释
- 范围触发
- 递归规则定时器
- 对象文本语法定时器
- 取消定时器
- 写在之后
在实际开发项目中,会遇到很多定时任务的工作。比如:定时导出某些数据、定时发送消息或邮件给用户、定时备份什么类型的文件等等
一般可以写个定时器,来完成相应的需求,在node.js中自已实现也非常容易,接下来要介绍的是node-schedule来完成定时任务
下面就用示例来说明一下node-schedule的用法。
node-schedule :https://github.com/node-schedule/node-schedule
安装:
npm install node-schedule
Cron风格定时器
var schedule = require('node-schedule');
function scheduleCronstyle(){
schedule.scheduleJob('30 * * * * *', function(){
console.log('scheduleCronstyle:' + new Date());
});
}
scheduleCronstyle();
var schedule = require('node-schedule');
function scheduleCronstyle(){
schedule.scheduleJob('30 * * * * *', function(){
console.log('scheduleCronstyle:' + new Date());
});
}
scheduleCronstyle();
schedule.scheduleJob的回调函数中写入要执行的任务代码,一个定时器就完成了!
下面我们再来讲讲Cron风格定时器传入的参数具体代表什么,先来看看上面执行结果,如下图
从输出结果可以看出,传入的'30 * * * * *'带来的结果是每分钟的30秒时都会执行,下面来看看这个传入参数分别代码什么
通配符解释
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ |
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └────────── day of month (1 - 31)
│ │ └─────────────── hour (0 - 23)
│ └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)
6个占位符从左到右分别代表:秒、分、时、日、月、周几
'*'表示通配符,匹配任意,当秒是'*'时,表示任意秒数都触发,其它类推
下面可以看看以下传入参数分别代表的意思
每分钟的第30秒触发: '30 * * * * *'
每小时的1分30秒触发 :'30 1 * * * *'
每天的凌晨1点1分30秒触发 :'30 1 1 * * *'
每月的1日1点1分30秒触发 :'30 1 1 1 * *'
2016年的1月1日1点1分30秒触发 :'30 1 1 1 2016 *'
每周1的1点1分30秒触发 :'30 1 1 * * 1'
这样很容易根据自已的需求用简短的代码去实现。
Cron风格定时器- 范围触发
上面的传入参数占位符中还可以传入范围,比如下面示例
var schedule = require('node-schedule');
function scheduleCronstyle(){
schedule.scheduleJob('1-10 * * * * *', function(){
console.log('scheduleCronstyle:' + new Date());
});
}
scheduleCronstyle();
结果如下图:
从输出结果可以看出每分钟的1-10秒都会触发,
其它占用符使用方法一样,输入范围可以看到参考前面"通配符解释"
递归规则定时器
再看看另一种风格写定时器
var schedule = require('node-schedule');
function scheduleRecurrenceRule(){
var rule = new schedule.RecurrenceRule();
// rule.dayOfWeek = 2;
// rule.month = 3;
// rule.dayOfMonth = 1;
// rule.hour = 1;
// rule.minute = 42;
rule.second = 0;
schedule.scheduleJob(rule, function(){
console.log('scheduleRecurrenceRule:' + new Date());
});
}
scheduleRecurrenceRule();
结果如下图:
从结果中可以看出,每分钟第60秒时就会触发,其它规则可以看我注释中的代码,当然,也可以组合使用,达到需求效果!
对象文本语法定时器
直接看使用示例
var schedule = require('node-schedule');
function scheduleObjectLiteralSyntax(){
//dayOfWeek
//month
//dayOfMonth
//hour
//minute
//second
schedule.scheduleJob({hour: 16, minute: 11, dayOfWeek: 1}, function(){
console.log('scheduleObjectLiteralSyntax:' + new Date());
});
}
scheduleObjectLiteralSyntax();
结果如下图:
代码实现的是每周一的下午16:11分触发,其它组合可以根据我代码中的注释参数名自由组合
取消定时器
示例如下,定时器对象的cancl方法即可
var schedule = require('node-schedule');
function scheduleCancel(){
var counter = 1;
var j = schedule.scheduleJob('* * * * * *', function(){
console.log('定时器触发次数:' + counter);
counter++;
});
setTimeout(function() {
console.log('定时器取消')
j.cancel();
}, 5000);
}
scheduleCancel();
结果如下:
写在之后
定时器功能大部分需求都可以借助node-schedule完成了,用它在项目中使用效果也不错,各种需求可以满足^_^!






