Reputation: 960
I have some functions, for example, afoo()
, bfoo()
and cfoo()
, I want that functions run simultaneously/paralel in a loop with different timing.
Example:
afoo()
--> run one time every 5 minutes
bfoo()
--> run one time every 10 minutes
cfoo()
--> run one time every hour
Is there anyway to do that? Maybe with SetInterval()?
Upvotes: 0
Views: 50
Reputation: 8351
You could use setInterval method to call a function on every fixed time delay, in the most case it's used in conjunction with clearInterval method to stop the repeated calls, here is an example:
var interval = setInterval( function () {
afoo( function (err) {
// clear interval if an error occured
if ( err ) {
console.log(err);
clearInterval(interval);
}
});
}, 5 * 60 * 1000);
If you want more control then use node-cron module, it has the same cron pattern used by linux systems, Example:
const CronJob = require('cron').CronJob;
// run afoo function every 15 min
var job = new CronJob('00 15 * * * *', afoo);
Upvotes: 1