node-schedule not executing on time

929 Views Asked by At

I want to create a bot that sends out a tweet everyday at 11:11. However, the code that I have written only runs once when I type in command node bot.js, it does not run after that. I did not create an object Job since the documentation says I can run scheduleJob() without creating the object manually.

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

var writeTweet = function() {
    Twitter.post('statuses/update', {
        status: messages[messageLocation]
    }, function(err, data, response) {
        console.log(data)
    });
    messageLocation += 1;
}
 
var j=schedule.scheduleJob('11 11 * * *',writeTweet());```
2

There are 2 best solutions below

0
On BEST ANSWER

It seems you are calling the function writeTweet() in your scheduleJob

Where as you just need to pass a function.

Like this:

var j=schedule.scheduleJob('11 11 * * *',writeTweet);

Check this out: https://www.npmjs.com/package/node-schedule

0
On

please check the time zone.

and i advice you to use cron instead of node-schedule. it gives you more control.

  1. you can change cron time without changing the code(SetTime)
  2. you have a lot of methods for this library like fireOnTick addCallback etc.
  3. can start and stop job dynamically.
  4. you have access to onTick and onComplete functions https://www.npmjs.com/package/cron