admin管理员组文章数量:1794759
使用node.js模块 node
1.安装 命令: npm install node-schedule 2.使用
node-schedule做定时任务有《两种》方式可以选择
2.1 Date-based Scheduling 方式1、确定时间,例如:2017年11月21日,5:30
var schedule = require('node-schedule'); var date = new Date(2017, 11, 21, 5, 30, 0); var j = schedule.scheduleJob(date, function(){ console.log('The world is going to end today.'); }); j.cancel(); //取消预设计划2、每小时的固定分钟,例如:每个小时的42分钟
var schedule = require('node-schedule'); var rule = new schedule.RecurrenceRule(); rule.minute = 42; var j = schedule.scheduleJob(rule, function(){ console.log('The answer to life, the universe, and everything!'); });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('Today is recognized by Rebecca Black!'); });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); });2.2 Cron-style Scheduling 方式
时间格式如下图:6个星号
1、每小时的第42分钟执行 var schedule = require('node-schedule'); //时间格式为 ‘42 * * * *’ 或者 ‘0 42 * * * *’, var j = schedule.scheduleJob('42 * * * *', function(){ console.log('The answer to life, the universe, and everything!'); });2、每周五的12:42:00执行
var schedule = require('node-schedule'); //时间格式为 ‘42 12 * * 5’ 或者 ‘0 42 12 * * 5’, var j = schedule.scheduleJob('42 12 * * 5', function(){ console.log('The answer to life, the universe, and everything!'); }); 3.官方网站www.npmjs/package/node-schedule
版权声明:本文标题:使用node.js模块 node 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686477556a71908.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论