MarsOnly
MarsOnly

Reputation: 1005

how to Start the job right now with node-cron

I was using node-cron to do something.I write the code like the node-cron demo below :

var cronJob = require('cron').CronJob;
var myJob = new cronJob('0 */10 * * * *', function(){
    console.log(new Date().toLocaleString());
},function(){

},true);
console.log('now is ' + new Date().toLocaleString());
myJob.start();

Iwant to start the job right now when i start, so the third args i make it true.But it dosen't work.What should i do to start the job right now?

Upvotes: 1

Views: 5561

Answers (1)

alandarev
alandarev

Reputation: 8635

Iwant to start the job right now when i start, so the third args i make it true.

Fourth argument, not third*

myJob.start();

This line is unnecessary, and might cause some side effects. As you already started your job in constructor by setting 4th argument to true.

But the code you shown looks valid. What makes you think it does not work? You have set it to run each 10 minutes, have you waited for clock to be XX:X0:00 to see if the code?

For sake of testing, you could change '0 */10 * * * *' to '* * * * * *' to see if the code works without waiting.

Upvotes: 2

Related Questions