Node. js USES node schedule to implement a timed task instance

  • 2020-03-30 03:12:05
  • OfStack

Sometimes, according to the business needs, this may be helpful when some operations are performed after midnight. I am currently studying this and welcome to discuss with you.

Making address: https://github.com/mattpat/node-schedule

A, install,


npm install node-schedule


Two, determine the time, for example: November 21, 2012, 5:30

var schedule = require('node-schedule');
var date = new Date(2012, 11, 21, 5, 30, 0);
var j = schedule.scheduleJob(date, function(){
    console.log('The world is going to end today.');
});
 Cancel default plan 

[code]
j.cancel();


A fixed number of minutes per hour, for example, 42 minutes per hour

var schedule = require('node-schedule');
var rule = new schedule.RecurrenceRule();
rule.minute = 42;
var j = schedule.scheduleJob(rule, function(){
    console.log('The answer to life, the universe, and everything!');
});

A certain time of day on certain days of the week, e.g. every Thursday, Friday, Saturday, Sunday at 17:00

var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [0, new schedule.Range(4, 6)];
rule.hour = 17;
rule.minute = 0;
var j = schedule.scheduleJob(rule, function(){
    console.log('Today is recognized by Rebecca Black!');
});

Execution per second


  var rule = new schedule.RecurrenceRule();
  var times = [];
  for(var i=1; i<60; i++){
    times.push(i);
  }
  rule.second = times;
  var c=0;
  var j = schedule.scheduleJob(rule, function(){
        c++;
        console.log(c);
  });


Related articles: