# node-schedule-test **Repository Path**: icode2017/node-schedule-test ## Basic Information - **Project Name**: node-schedule-test - **Description**: node.js 定时任务模块 node-schedule 的使用 - **Primary Language**: JavaScript - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2017-02-16 - **Last Updated**: 2021-03-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # node.js 定时任务:node-schedule 的使用 node-schedule 用的是 Cron 表达式来进行设定的, ``` * * * * * * ┬ ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ | │ │ │ │ │ └ 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) ``` ## Cron 语法格式 * `Seconds` `Minutes` `Hours` `DayofMonth` `Month1` `DayofWeek` `Year` * `Seconds` `Minutes` `Hours` `DayofMonth` `Month1` `DayofWeek` 每一个域可出现的字符如下: `Seconds` 可出现 `,` `–` `*` `/` 四个字符,有效范围为 0-59 的整数 `Minutes` 可出现 `,` `–` `*` `/` 四个字符,有效范围为 0-59 的整数 `Hours` 可出现 `,` `–` `*` `/` 四个字符,有效范围为 0-23 的整数 `DayofMonth` 可出现`,` `–` `*` `/` `?` `L` `W` `C`八个字符,有效范围为 0-31 的整数 `Month` 可出现 `,` `–` `*` `/` 四个字符,有效范围为 1-12 的整数或 JAN-DEc `DayofWeek` 可出现 `,` `–` `*` `/` `?` `L` `C` `#` 八个字符,有效范围为 1-7 的整数或 SUN-SAT 两个范围。1 表示星期天,2 表示星期一, 依次类推 `Year` 可出现 `,` `–` `*` `/` 四个字符,有效范围为 1970-2099 年 每一个域都使用数字,但还可以出现如下特殊字符,它们的含义是: 1. `*` 表示匹配该域的任意值,假如在 `Minutes` 域使用 `*` , 即表示每分钟都会触发事件。 2. `?` 只能用在 `DayofMonth` 和 `DayofWeek` 两个域。它也匹配域的任意值,但实际不会。因为 `DayofMonth` 和 `DayofWeek` 会相互影响。例如想在每月的 20 日触发调度,不管 20 日到底是星期几,则只能使用如下写法: 13 13 15 20 * ?, 其中最后一位只能用 `?`,而不能使用 `*`,如果使用 `*` 表示不管星期几都会触发,实际上并不是这样。 3. `-` 表示范围,例如在 `Minutes` 域使用 `5-20`,表示从 5 分到 20 分钟每分钟触发一次 4. `/` 表示起始时间开始触发,然后每隔固定时间触发一次,例如在 `Minutes` 域使用 `5/20`,则意味着 5 分钟触发一次,而 25,45 等分别触发一次. 5. `,` 表示列出枚举值值。例如:在 `Minutes` 域使用 `5,20`,则意味着在 5 和 20 分每分钟触发一次。 6. `L` 表示最后,只能出现在 `DayofWeek` 和 `DayofMonth` 域,如果在 `DayofWeek` 域使用 `5L`,意味着在最后的一个星期四触发。 7. `W` 表示有效工作日(周一到周五),只能出现在 `DayofMonth` 域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 `DayofMonth` 使用 `5W`,如果 5 日是星期六,则将在最近的工作日:星期五,即 4 日触发。如果 5 日是星期天,则在 6 日(周一)触发;如果 5 日在星期一到星期五中的一天,则就在 5 日触发。另外一点,W 的最近寻找不会跨过月份 8. `LW` 这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。 9. `#` 用于确定每个月第几个星期几,只能出现在 `DayofMonth` 域。例如在 `4#2`,表示某月的第二个星期三。 > node-schedule 暂时不支持 `L`、`W` 和 `#` 这三种设定值 ```javascript var schedule = require('node-schedule'); var j = schedule.scheduleJob('42 * * * *', function(){ console.log('The answer to life, the universe, and everything!'); }); ``` ## 计划类型 ### 在确定时间执行 ### 按固定时间间隔循环执行 ### 一个星期中的某些天的某个时刻执行 例如:周一到周日的20点执行 ```javascript var rule = new schedule.RecurrenceRule(); rule.dayOfWeek = [0, new schedule.Range(1, 6)]; rule.hour = 20; rule.minute = 0; var j = schedule.scheduleJob(rule, function(){ console.log("执行任务"); }); ``` ### 每秒执行 ```javascript 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); }); ``` ## 参考: * [node.js定时任务:node-schedule的使用](http://www.cnblogs.com/ajun/p/3548259.html) * [使用node-schedule时的注意点](http://www.tuicool.com/articles/2umMBfz) * [在线Cron表达式生成器](http://cron.qqe2.com/) * []()