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)

  • 1
    There are so many posts about this. `setInterval` will do the job for this `setInterval(function(){ yourFunction() }, 28800000)` – Hakan Kose Dec 04 '17 at 00: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 Dec 04 '17 at 00:19
  • You could also use a chron tool to just run your node.js program from scratch every 8 hours. – jfriend00 Dec 04 '17 at 00:22
  • Check out this post? https://superuser.com/questions/139401/making-a-command-run-once-every-hour – Skam Dec 04 '17 at 00: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. Dec 04 '17 at 01:22

2 Answers2

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();
     */
});
Kalana Demel
  • 3,220
  • 3
  • 21
  • 34
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. */
);
Gnanesh
  • 668
  • 7
  • 13