0

I am trying to impliment into my nodejs script a function to allow once per 8 hours a select command.

example:

!hug <--- would let bot respond with a hug but only once every 8 hours

I've been scouring online but cannot find what I need... I am trying to get it as simplified as possible.. (i.e without mongo... etc)

5
  • 1
    There are so many posts about this. setInterval will do the job for this setInterval(function(){ yourFunction() }, 28800000)
    – Hakan Kose
    Commented Dec 4, 2017 at 0:19
  • Are you just looking for setInterval() set for every 8 hours? Then you just run your node.js program, start the interval timer and it will fire every 8 hours where you can then run whatever code you want.
    – jfriend00
    Commented Dec 4, 2017 at 0:19
  • You could also use a chron tool to just run your node.js program from scratch every 8 hours.
    – jfriend00
    Commented Dec 4, 2017 at 0:22
  • Check out this post? superuser.com/questions/139401/…
    – Skam
    Commented Dec 4, 2017 at 0:22
  • Store the last use time of the command and then when the command comes in compare to see if the last use time is over 8 hours ago if so set the last use time to now and do the command. Otherwise don't.
    – Dan D.
    Commented Dec 4, 2017 at 1:22

2 Answers 2

0

You can use node-schedule for this and for more versatility where you can configure days, hours and minutes and cancel on particular conditions being met this also gives you to use cron expressions as well.

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

var rule = new schedule.RecurrenceRule();
//Will run at 1am 9am and 5pm
rule.hour = [1, 9, 17];

var task = schedule.scheduleJob(rule, function(){
     //Do Stuff
     console.log("Scheduled Task Running...")
     /*
     if(condition met)
        task.cancel();
     */
});
0

You can use node-cron

var CronJob = require('cron').CronJob;
var job = new CronJob('00 30 11 * * 1-5', function() {
  /*
   * Runs every weekday (Monday through Friday)
   * at 11:30:00 AM. It does not run on Saturday
   * or Sunday.
   */
  }, function () {
    /* This function is executed when the job stops */
  },
  true, /* Start the job right now */
  timeZone /* Time zone of this job. */
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.