node-schedule 完成定时使命使用方法纪录

在项目中有个天天0点实行的函数,原本想用setInterval来完成,但以为这类需求今后应当还会有,本身写能够拓展性不高。
搜了一下发现了node-schedule这个包。
如今纪录一下运用要领

node-schedule没次都是经由过程新建一个scheduleJob对象来实行具体要领。

时候数值按下表示意

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    |
│    │    │    │    │    └ [dayOfWeek]day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── [month]month (1 - 12)
│    │    │    └────────── [date]day of month (1 - 31)
│    │    └─────────────── [hour]hour (0 - 23)
│    └──────────────────── [minute]minute (0 - 59)
└───────────────────────── [second]second (0 - 59, OPTIONAL)

运用node-schedule在指定时候实行要领

var schedule = require('node-schedule');
var date = new Date(2015, 11, 16, 16, 43, 0);

var j = schedule.scheduleJob(date, function(){
  console.log('如今时候:',new Date());
});

在2015年12月16日16点43分0秒,打印当时时候

指定时候距离实行要领

var rule = new schedule.RecurrenceRule();
rule.second = 10;
var j = schedule.scheduleJob(rule, function(){
  console.log('如今时候:',new Date());
});

这是每当秒数为10时打印时候。假如想每隔10秒实行,设置 rule.second =[0,10,20,30,40,50]即可。
rule支撑设置的值有second,minute,hour,date,dayOfWeek,month,year
同理:
每秒实行就是rule.second =[0,1,2,3……59]
每分钟0秒实行就是rule.second =0
每小时30分实行就是rule.minute =30;rule.second =0;
天天0点实行就是rule.hour =0;rule.minute =0;rule.second =0;
….
每个月1号的10点就是rule.date =1;rule.hour =10;rule.minute =0;rule.second =0;
每周1,3,5的0点和12点就是rule.dayOfWeek =[1,3,5];rule.hour =[0,12];rule.minute =0;rule.second =0;
….

    原文作者:裴大鱼
    原文地址: https://segmentfault.com/a/1190000004155848
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞