在NodeJs中使用node-schedule增加定时器任务的方法

所属分类: 网络编程 / JavaScript 阅读数: 193
收藏 0 赞 0 分享

java中直接使用定时器类就行了,但是在node中就没有这么简单了,只能使用setInterval或者setTimeout两个方法来实现,但是太繁琐了,搜索了之后发现node-schedule这个包,特意试用一下

版本

node版本12.16.2koa2版> 2.7.0

1. 安装

npm insatll node-schedule -S

2. 使用方法

2-1. 调用格式

// 任务名称可以用中文,也可以用英文,但必须唯一
schedule.scheduleJob(`任务名称`, `时间`, () => {
});

2-2. 时间格式

  • 每分钟的第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'

3. 在项目中使用

3-1. 建立schedule.js

const schedule = require('node-schedule');
// 生成新的定时任务
let interval = async (options) => {
 return new Promise((resolve) => {
 // 这里设定14天为一个循环周期
 // 假如设定的日期是2020-06-08, 返回就是{year: 2020, month: 6, day: 22, hour: 8, min: 0}
 let time14 = GetDateStr(options.maintain_time, 14)
 console.log(`${options.unit_name}_${time14.year}-${time14.month}-${time14.day}`, `1-2 1 1 ${time14.day} ${time14.month} *`)
 // 终止之前的定时任务
 editMaintainTime(options)
 // 按照固定格式,设定定时任务,这里使用每条数据的唯一字段+定时任务时间,作为任务名称存入定时任务列表中
 / 任务名称就是'名字_2020-6-22'
 // 任务时间就是'1-2 1 1 22 6 *' ,意思是每年的6月22日的每小时的1秒~10秒触发
 schedule.scheduleJob(`${options.unit_name}_${time14.year}-${time14.month < 10 ? "0" + time14.month: time14.month}-${time14.day < 10 ? "0" + time14.day: time14.day}`, `1-10 * * ${time14.day} ${time14.month} *`, () => {
 console.log(options,'The world is going to end today.' + new Date())
 // 写入你自己想在定时任务触发的时候,想要执行的函数
 });
}
// 删除定时任务
let editMaintainTime = async (options) => {
 console.log('options', options)
 // 查看所有的定时任务
 for (let i in schedule.scheduledJobs) {
 console.error("任务删除前:"+i);
 }
 // 终止之前的定时任务
 console.log('终止的任务', `${options.alarm14}`)
 if (schedule.scheduledJobs[`${options.alarm14}`]) {
 schedule.scheduledJobs[`${options.alarm14}`].cancel();
 }
 // 查看剩下的定时任务
 for (let i in schedule.scheduledJobs) {
 console.error("任务删除后:"+i);
 }
 // time.cancel()
 console.log('删除成功')
}
// 时间选择
let GetDateStr = (maintain_time, AddDayCount) => {
 var dd = new Date(`${maintain_time}`);
 dd.setDate(dd.getDate() + AddDayCount); // 获取AddDayCount天后的日期
 var y = dd.getFullYear(); 
 var m = dd.getMonth() + 1
 var d = dd.getDate()
 var h = dd.getHours()
 var min = dd.getMinutes()
 return {
 year: y,
 month: m,
 day: d,
 hour: h,
 min: min,
 }
}
const intervalControl = {
 interval: interval
}
module.exports = intervalControl

3-2. 调用该方法

const intervalControl = require('../util/schedule')

// options传入{unit_name: '名字', maintain_time: '自己选择的开始时间', alarm14: '上一次定时任务的任务名称'}
// unit_name,无格式
// maintain_time:2020-06-08
// alarm14: 2020-06-22
intervalControl.interval(options)
更多精彩内容其他人还在看

jquery实现可拖拽弹出层特效

这篇文章主要介绍了jquery实现可拖拽弹出层特效,代码非常精简,效果非常棒,这里推荐给有需要的小伙伴
收藏 0 赞 0 分享

浅谈 javascript 事件处理

本文向大家简单介绍了javascript的事件处理机制,从事件源,事件操作到事件处理程序都做了简单介绍,并给出了部分示例,这里推荐给大家。
收藏 0 赞 0 分享

jQuery中:reset选择器用法实例

这篇文章主要介绍了jQuery中:reset选择器用法,实例分析了:reset选择器的功能、定义及匹配重置按钮的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

原生javascript实现隔行换色

这篇文章主要介绍了原生javascript实现隔行换色,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery中:button选择器用法实例

这篇文章主要介绍了jQuery中:button选择器用法,实例分析了:button选择器的功能、定义及选取按钮的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery中:file选择器用法实例

这篇文章主要介绍了jQuery中:file选择器用法,实例分析了:file选择器的功能、定义及选取类型为file的<input>元素的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery中:enabled选择器用法实例

这篇文章主要介绍了jQuery中:enabled选择器用法,实例分析了:enabled选择器的功能、定义及选取所有可用元素的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

AngularJS中取消对HTML片段转义的方法例子

这篇文章主要介绍了AngularJS中取消对HTML片段转义的方法例子,在一些需要显示HTML的地方,就要取消AngularJS的转义,本文就介绍了这种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery中:disabled选择器用法实例

这篇文章主要介绍了jQuery中:disabled选择器用法,实例分析了:disabled选择器功能、定义及选取所有禁用的表单元素的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery中:checked选择器用法实例

这篇文章主要介绍了jQuery中:checked选择器用法,实例分析了:checked选择器的功能、定义及选取选中的复选框或单选按钮时的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多