Introduction to the

In Java, you can use the timer class directly, but in Node, it is not so easy. You can only use the setInterval or setTimeout methods to achieve this, but it is too tedious. After searching, I found the package node-schedule, so I specially try it

version

The node version 12.16.2

> koa2 version 2.7.0

1. Install

npm insatll node-schedule -S
Copy the code

2. Usage

2-1. Call format

Schedule. scheduleJob(' task name ', 'time', () => {});Copy the code

2-2. Time format

  • 30th second trigger per minute: ’30 * * * * *’
  • Trigger at 1:30 per hour: ’30 1 * * * *’
  • Trigger every day at 1:1:30 am: ’30 1 1 * * *’
  • Trigger at 1:1:30 on 1st of each month: ’30 1 1 1 1 * *’
  • January 1, 2016 triggered at 1:1:30: ’30 1 1 1 2016 *’
  • Trigger at 1:1:30 on 1 of a week: ’30 1 1 * * 1′

3. Use it in your project

3-1. Set up the schedule. Js

const schedule = require('node-schedule'); Generates a new scheduled tasklet interval = async (options) => {
  returnNew Promise(resolve) => {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}* ') // Terminates the previous scheduled task editMaintainTime(options) // Sets the scheduled task in a fixed format. The unique field of each data plus the scheduled task time is used as the task name'name _2020-6-22'// The task time is'1-2 1 1 22 6 'Schedule. scheduleJob(') is triggered 10 times at 1:01 on June 22 every year${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()) // Write the function you want to execute when the scheduled task is triggered}); } // Delete a scheduled tasklet editMaintainTime = async (options) => {
    console.log('options', options) // View all scheduled tasksfor (let i in schedule.scheduledJobs) {
      console.error("Before task deletion:"+i); } // Terminate the previous scheduled task console.log('Terminated task', `${options.alarm14}`)
    if (schedule.scheduledJobs[`${options.alarm14}`]) {
      schedule.scheduledJobs[`${options.alarm14}`].cancel(); } // View the remaining scheduled tasksfor (let i in schedule.scheduledJobs) {
      console.error("Task deleted:"+i);
    }
    // time.cancel()
    
     console.log('Deleted successfully'} // Time selectionlet GetDateStr = (maintain_time, AddDayCount) => {
  var dd = new Date(`${maintain_time}`); dd.setDate(dd.getDate() + 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
Copy the code

3-2. Call this method

const intervalControl = require('.. /util/schedule') // options passed in {unit_name:'name', maintain_time: 'Start time of your choice', alarm14: 'Task name of last scheduled task'} // unit_name, unformatted // Maintain_time: 2020-06-08 // alarm14: 2020-06-22 IntervalControl. interval(options)Copy the code