-
Now, if I run the below script, the function will run after 5 seconds, instead of calling the function once before creating the Cronjob, is there any other way to run the function at the beginning first and then run every 5 seconds? import { CronJob } from 'cron';
const job = new CronJob(
'*/5 * * * * *', // cronTime
function () {
console.log('You will see this message every 5 second');
}, // onTick
null, // onComplete
true, // start
'America/Los_Angeles' // timeZone
); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
As specified in the API documentation, you can use the import { CronJob } from 'cron';
const job = new CronJob(
'*/5 * * * * *', // cronTime
function () {
console.log('You will see this message every 5 second');
}, // onTick
null, // onComplete
true, // start
'America/Los_Angeles', // timeZone
null, // context
true // runOnInit
); On a side note, because it is more explicit and does not care about the order of options, I would recommend using the import { CronJob } from 'cron';
const job = CronJob.from({
cronTime: '*/5 * * * * *',
onTick: function () {
console.log('You will see this message every 5 second');
},
start: true,
timeZone: 'America/Los_Angeles',
runOnInit: true
}); |
Beta Was this translation helpful? Give feedback.
-
Thank you very much, especially for the side note, very helpful! |
Beta Was this translation helpful? Give feedback.
As specified in the API documentation, you can use the
runOnInit
option to meet that requirement:On a side note, because it is more explicit and does not care about the order of options, I would recommend using the
CronJob.from()
static method to declare your jobs: