I used node-schedule to schedule a task executed only once for all at particular time. The doc shows that for me:
Date-based Scheduling: Say you very specifically want a function to execute at 5:30am on December 21, 2012. Remember - in JavaScript - 0 - January, 11 - December.
const schedule = require('node-schedule'); const date = new Date(2012, 11, 21, 5, 30, 0); const job = schedule.scheduleJob(date, function(){ console.log('The world is going to end today.'); });
But this code runs the task every minute, I just want it to run the task only once at the particular time specified as here:
const date = new Date(2012, 11, 21, 5, 30, 0);
The date here is a default. I changed it for future date in my code as below:
const now = new Date().getTime();
const seconds = 180;
const parsedDate = new Date(Date.parse(now));
const date = new Date(parsedDate.getTime() + (1000 * seconds));
schedule.scheduleJob(date, function(data) {
console.log("Job ran @", new Date().toString());
});
Here I just want to cron a job after 180 seconds (3 minute) from current date. But console result shows that console log is written every minutes after deploy:
> Job ran @ Tue Mar 16 2021 08:56:00 GMT+0000 (GMT)
> Job ran @ Tue Mar 16 2021 08:57:00 GMT+0000 (GMT)
> Job ran @ Tue Mar 16 2021 08:58:00 GMT+0000 (GMT)
> Job ran @ Tue Mar 16 2021 08:59:00 GMT+0000 (GMT)
Thanks for your help.