node-schedule 全局内关闭定时器

发布时间:2019-06-22 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了node-schedule 全局内关闭定时器脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

Cron表达式

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

用Cron表达式完成定时器

schedule.scheduleJob('0 1 * * *', () => {
 // something...
})

关闭定时器

API

let obj = schedule.scheduleJob('0 1 * * *', () => {
 // something...
})

obj.close();

全局内关闭定时器 -- 疑问

全局内关闭定时器需要获取到定时器的引用

看源码第607行

var name = (arguments.length >= 3 && typeof arguments[0] === 'string') ? arguments[0] : null;
  var spec = name ? arguments[1] : arguments[0];
  var method = name ? arguments[2] : arguments[1];
  var callback = name ? arguments[3] : arguments[2];

scheduleJob存在第四个参数,然而readme中没有提及,可知API

scheduleJob(name, spec, method, callback)
// name: 定时器的key值 spec: Cron表达式  method: method  callback: callback

全局内关闭定时器 -- 解决

  • 首先定义定时器
scheduleJob(name, spec, method, callback)
  • 在需要关闭的地方,写如下代码
function repeatSchedule(str) {
  for (var i in nodeschedule.scheduledJobs) {
    // 对比key值, key相同则重复
    if (nodeschedule.scheduledJobs[i].name.indexOf(str) > 0) {
      nodeschedule.scheduledJobs[i].close();
    }
  }
}

脚本宝典总结

以上是脚本宝典为你收集整理的node-schedule 全局内关闭定时器全部内容,希望文章能够帮你解决node-schedule 全局内关闭定时器所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: