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)
总结
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年09月21日
2024年09月21日
- 黑神话悟空上品狸侍长精魄获取方法一览|上品狸侍长精魄收集攻略
- 大自然环保音乐系列《风花雪月》4CD/DTS[WAV分轨]
- 模拟之声慢刻CD甄妮《86精选[日本母带]》[低速原抓WAV+CUE]
- [ABC]安娜-胆麦发烧女声[6N纯银镀膜][2016[低速原抓WAV+CUE]
- 任天堂今晚举行直面会!第三方及独立游戏展示
- 《哆啦A梦的铜锣烧店物语》发售!开罗公式+哆啦A梦
- 任天堂公布《塞尔达传说》系列时间线:野炊与王泪独立在外
- 五条人.2012-一些风景2CD【刀马旦】【WAV+CUE】
- 陈奕迅.2013-Easons.Life演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 许美静.1995-遗憾(新马版)【上华】【WAV+CUE】
- 《叶倩文 歌声情缘》[WAV+CUE][410MB]
- 《张国荣 首首动听经典不容错过 追忆的风 2CD》[WAV+CUE][870MB]
- 《腾格尔 容中尔甲 亚东 高原三星 男人篇 3CD》[WAV/分轨][1GB]
- 命运圣契公测实测可用兑换码大全 命运圣契最新兑换码分享
- 黑神话悟空上品疾蝠精魄获取方法一览|上品疾蝠精魄收集攻略