Say I wanted to set up a timed service to run via Node. Would I really be using the
setTimeout
setInterval
setInterval(function(){
// using moment.js for time formatting
var isItTime = new moment().format("HH:mm") === "04:00";
if( isItTime ) {
sendEmail();
}
}, 60 * 1000);
I'm not normally one to recommend packages, but since this is a relatively common pattern, ie search for "node cron job", I would recommend leveraging a package for scheduling repeating tasks. One such example is node-schedule.
See this quote from the docs as to why I recommend it, (emphasis mine):
Node Schedule is for time-based scheduling, not interval-based scheduling. While you can easily bend it to your will, if you only want to do something like "run this function every 5 minutes", you'll find setInterval much easier to use, and far more appropriate. But if you want to, say, "run this function at the :20 and :50 of every hour on the third Tuesday of every month," you'll find that Node Schedule suits your needs better.