-1

I want to schedule a function in a future. This is my code:

var timeout = setTimeout(function() {
                function();
                console.log("UNO");
            }, 5000);

But I don't like this solution, so Anyone know some method in nodejs that I use to schedule a function in the future?

Picco
  • 423
  • 2
  • 6
  • 21

3 Answers3

2

There is this: https://www.npmjs.com/package/node-schedule

Also, you could set interval of 1 second (or millisecond or hour or whatever depending on precision you need) and inside of it check current time. If time is right, you trigger function.

Rastko
  • 477
  • 2
  • 7
1

Use can use node-schedule package

var scheduler = require('node-schedule');

var date = new Date(2018, 1, 1, 0, 0, 0);

var newYearJob = scheduler.scheduleJob(date, function() {
 console.log("Happy new year");
});

In case not needed

newYearJob.cancel();
amardeep k
  • 21
  • 1
  • 2
0

You can use: https://www.npmjs.com/package/agenda

With this package the scheduler will still work after system or script restart thanks to persitant storage (MongoDB, Redis, ...)

CptHash
  • 1
  • 2