node.js使用定时任务

2023/2/22 schedule

原文地址 (opens new window) 在实际开发项目中,会遇到很多定时任务的工作。比如:定时导出某些数据、定时发送消息或邮件给用户、定时备份什么类型的文件等等

一般可以写个定时器,来完成相应的需求,在node.js中自已实现也非常容易,接下来要介绍的是node-schedule来完成定时任务

下面就用示例来说明一下node-schedule的用法。

# 安装

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

# 用法

  1. Cron风格定时器
const schedule = require('node-schedule');

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

scheduleCronstyle();
1
2
3
4
5
6
7
8
9
10

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)
1
2
3
4
5
6
7
8
9

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' 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();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  1. 取消定时器

调用 定时器对象的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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

参考 (opens new window)

最后更新时间: 2023/2/23 18:24:24