NPM cronJob - 动态设置时间

NPM cronJob - dynamically set time

这是我安排任务的代码。我使用了不同的路线来启动、停止和更改时间,如下所示。请告诉我它是否正确。而且我在更改时间时遇到错误 frequency.please 帮助我。

const cronjob=require('cron').CronJob

var first='* * * * * *'

var task=function() {

    console.log('last'+job.lastDate()+' next '+job.nextDates(1));
  }

  var complete=function(){
      console.log('the end.........')
  }

var job = new cronjob(first,task, complete, false, 'Asia/Kolkata');

app.get('/start',(req,res)=>{

job.start()

console.log('is job running? ', job.running);

res.send('cron started')

})


app.get('/set',(req,res)=>{

    var time=req.headers.time    /* input taken from user for changing the frequency*/

    time='30 5 1-31 0-11 1-7'  /*hard-coded temporary for testing*/

    console.log(time)

    job.setTime(time)

})

/set api 抛出错误 /错误:时间必须是 CronTime 的一个实例。/

我们必须使用 cronTime 的实例,即

const CronTime = require('cron').CronTime
.
.
.
var time=req.query.time
//time='*/5 * * * * *'
job.setTime(new CronTime(time))
res.send('time changes')