文章目录

  • ​​git​​
  • ​​参数​​
  • ​​定时任务​​
  • ​​每天0点执行一次数据统计任务​​
  • ​​每半个小时执行一次数据统计任务​​
  • ​​重复任务​​
  • ​​指定执行时间氛围​​
git

​https://github.com/node-schedule/node-schedule#readme​

const schedule = require('node-schedule');
参数

node.js 定时任务/重复任务_定时任务


可见第一个参数决定绅什么时候执行 可以是 string number 重复规则 重复时间区域 重复对象列表 Date 十分全面可适应大部分常见场景

定时任务

每天0点执行一次数据统计任务
schedule.scheduleJob('0 0 0 * * *', function () {
//.....
})
每半个小时执行一次数据统计任务
schedule.scheduleJob('0 0 0 0 30 *', function () {
//.....
})

重复任务

指定执行时间氛围

例如 允许 7点到12点执行

const rule = new schedule.RecurrenceRule();
rule.hour = [7,8,9,12];

如上实际效果会是 在7点到12点期间一直执行
7点到12点每1个小时的0分0秒执行一次

const rule = new schedule.RecurrenceRule();
rule.hour = [7,8,9,12];
rule.minute = 0;
rule.second = 0;

7点到12点每半个小时执行一次

const rule = new schedule.RecurrenceRule();
rule.hour = [7,8,9,12];
rule.minute = [0,30];
rule.second = 0