【问题标题】:NPM cronJob - dynamically set timeNPM cronJob - 动态设置时间
【发布时间】:2020-09-18 13:28:35
【问题描述】:

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

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 的实例。/

【问题讨论】:

    标签: node.js cron job-scheduling cron-task node-schedule


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-03-27
      • 2023-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      相关资源
      最近更新 更多