Nodejs定时任务(node-schedule)

node-schedule 官方

Nodejs学习笔记(十二)— 定时任务(node-schedule)

Nodejs定时任务(node-schedule)

安装:

npm install node-schedule --save 
# 或者 
yarn add node-schedule

用法

1、Cron风格定时器

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

const  scheduleCronstyle = ()=>{
  //每分钟的第30秒定时执行一次:
    schedule.scheduleJob('30 * * * * *',()=>{
        console.log('scheduleCronstyle:' + new Date());
    }); 
}

scheduleCronstyle();

schedule.scheduleJob的回调函数中写入要执行的任务代码,一个定时器就完成了!

规则参数讲解 *代表通配符

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    │
│    │    │    │    │    └ 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'

每个参数还可以传入数值范围:

const task1 = ()=>{
  //每分钟的1-10秒都会触发,其它通配符依次类推
  schedule.scheduleJob('1-10 * * * * *', ()=>{
    console.log('scheduleCronstyle:'+ new Date());
  })
}

task1()

2、对象文本语法定时器

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

function scheduleObjectLiteralSyntax(){

    //dayOfWeek
    //month
    //dayOfMonth
    //hour
    //minute
    //second
      //每周一的下午16:11分触发,其它组合可以根据我代码中的注释参数名自由组合
    schedule.scheduleJob({hour: 16, minute: 11, dayOfWeek: 1}, function(){
        console.log('scheduleObjectLiteralSyntax:' + new Date());
    });
   
}

scheduleObjectLiteralSyntax();

确定的时间执行

比如: 2016年7月13日15:50:00 , new Date() 的时候月份要减1.

var date = new Date(2016,6,13,15,50,0);
schedule.scheduleJob(date, function(){
  httpGet();
});

递归规则定时器

  • 秒为单位执行

比如:每5秒执行一次

var rule1     = new schedule.RecurrenceRule();
var times1    = [1,6,11,16,21,26,31,36,41,46,51,56];
rule1.second  = times1;
schedule.scheduleJob(rule1, function(){
  httpGet();
});
  • 以分为单位执行

比如:每5分种执行一次

var rule2     = new schedule.RecurrenceRule();
var times2    = [1,6,11,16,21,26,31,36,41,46,51,56];
rule2.minute  = times2;
schedule.scheduleJob(rule2, function(){
  httpGet();
});
  • 每分钟第60秒时就会触发
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();

3、取消定时器

调用 定时器对象的cancl()方法即可

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

function scheduleCancel(){

    var counter = 1;
    const j = schedule.scheduleJob('* * * * * *', function(){
        
        console.log('定时器触发次数:' + counter);
        counter++;
        
    });

    setTimeout(function() {
        console.log('定时器取消')
      // 定时器取消
        j.cancel();   
    }, 5000);
    
}

scheduleCancel();
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 `node-schedule` 模块可以很方便地测试一个时间是否满足 cron 表达式。 首先需要安装 `node-schedule` 模块,可以使用以下命令进行安装: ``` npm install node-schedule ``` 接下来,可以使用 `schedule.cronJob()` 方法创建一个 cron 任务,并设置它的 cron 表达式。然后,可以使用 `job.nextInvocation()` 方法获取下一次任务执行的时间,或者使用 `job.pendingInvocations()` 方法获取未来若干个执行时间。 以下是一个示例代码: ```javascript const schedule = require('node-schedule'); // 创建一个 cron 任务 const job = schedule.cronJob('0 0 * * * *', function() { console.log('执行任务'); }); // 获取下一次任务执行的时间 console.log(job.nextInvocation().toString()); // 判断一个时间是否满足 cron 表达式 const date = new Date('2022-01-01T00:00:00Z'); // 设置一个时间 if (job.schedule(date)) { console.log(`${date} 满足 cron 表达式`); } else { console.log(`${date} 不满足 cron 表达式`); } ``` 在上面的示例中,创建了一个 cron 任务,它的 cron 表达式为每秒执行一次。然后使用 `nextInvocation()` 方法获取了下一次任务执行的时间,并输出了结果。接下来,设置了一个时间,并使用 `schedule()` 方法判断它是否满足 cron 表达式,最后输出了结果。 注意,`schedule()` 方法会返回一个布尔值,表示给定的时间是否满足 cron 表达式。如果返回 `true`,则表示满足;如果返回 `false`,则表示不满足。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值