0

I am currently running a function at regular interval round the clock.

setInterval( function(){ do_this(); } , 1000*60);

Unfortunately, this is not exactly what I want. I would like this function to be run at set regular interval from morning 0900hrs to 1800hrs only. The function should not run outside of these hours. How can this be done in node.js? Are there convenient modules or functions to use?

guagay_wk
  • 26,337
  • 54
  • 186
  • 295
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCHours – pawel Nov 09 '15 at 08:32
  • 1
    Why the 2 negative votes? Please explain what is wrong with the question so that I can improve on my future questions. – guagay_wk Nov 09 '15 at 08:32
  • 1
    Well, you know how to schedule something to happen after a period of time, and you presumably know how to get the current time, so...what's the question? If it's a framework/library recommendation question, it's off-topic. Otherwise, show an attempt to solve the problem. *(not my dv, but I do understand them)* – T.J. Crowder Nov 09 '15 at 08:34
  • @ T.J. Crowder, My thinking process was too complicated. It didn't occur to me then, really. Sometimes, the simplicity of stack overflow answers make me feel stupid. – guagay_wk Nov 09 '15 at 08:39
  • 1
    @user781486 This question was helpful to me, and I used content from one of the answers inside my application. – steampowered Jul 31 '17 at 17:43

3 Answers3

5

You can simply just check to see if the current time is within the desired time range or not and use that to decide whether to execute your function or not.

setInterval( function(){ 
    var hour = new Date().getHours();
    if (hour >= 9 && hour < 18) {
        do_this(); 
    }
} , 1000*60);

This will run your function every minute between the hours of 9:00 and 18:00.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
2

Is there any specific framework you are working with?

If we're as abstract as this, you would most likely want to use something like a cronjob. There's a module for that: https://github.com/ncb000gt/node-cron

The pattern for what you want:

00 00 9-18 * * * - This will be ran each hour between 9-18 at exactly 0 minutes and 0 seconds.

Andrius
  • 5,934
  • 20
  • 28
  • `1000*60` is every 60 seconds ;-) – Cerbrus Nov 09 '15 at 08:38
  • @Cerbrus Shh.. I would expect the OP to actually do some work. I did not say that it does exactly what he needs it to do :) I guess, he'll still go with a `setInterval()`. – Andrius Nov 09 '15 at 08:41
  • 1
    While I can appreciate the incentive to make him learn something, cronjobs may be completely new to him. I think it's more educational to provide a working cronjob, explaining what each of the parameters is. Still, +1 for the alternative. – Cerbrus Nov 09 '15 at 08:49
  • I think setInterval() is more suitable. But I learnt something new today. cronjobs look good for longer interval tasks. – guagay_wk Nov 09 '15 at 08:56
1

Check for the current hour inside your do_this function.

function do_this(){
    var now = new Date();
    var currentHour = now.getHours();
    if(currentHour < 9 && currentHour > 18) return;
    //your code
}

setInterval( function(){ do_this(); } , 1000*60);
AdityaParab
  • 7,024
  • 3
  • 27
  • 40