Nikhil Savaliya
Nikhil Savaliya

Reputation: 2166

trigger function on specific time daily nodejs

I am Using Nodejs, and want trigger one function on 5:30 AM, 11:30 PM daily.

How should i follow that approach.

I also want to add more time apart from above like 9:45 PM, 5:30 PM

I have check https://www.npmjs.com/package/node-schedule tried but not getting any luck.

var j = schedule.scheduleJob('* * * 23 07 00', function () {
    console.log('The answer to life, the universe, and everything!');
});
j.schedule()

Thanks.

Upvotes: 2

Views: 5595

Answers (1)

Ilarion Halushka
Ilarion Halushka

Reputation: 2343

You have specified a wrong schedule pattern.

More readable way is using objects like this:

const j = schedule.scheduleJob({hour: 5, minute: 30}, () => {
  console.log('Job runs every day at 5:30AM');
});

And in case you need multiple Jobs running at different time of the day - create a few jobs with different time pattern and use the same function in each.

Upvotes: 5

Related Questions