admin管理员组

文章数量:1327102

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?

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?

Share Improve this question edited Nov 9, 2015 at 8:31 guagay_wk asked Nov 9, 2015 at 8:28 guagay_wkguagay_wk 28.1k64 gold badges200 silver badges309 bronze badges 5
  • developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – pawel Commented Nov 9, 2015 at 8: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 Commented Nov 9, 2015 at 8: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 remendation question, it's off-topic. Otherwise, show an attempt to solve the problem. (not my dv, but I do understand them) – T.J. Crowder Commented Nov 9, 2015 at 8:34
  • @ T.J. Crowder, My thinking process was too plicated. It didn't occur to me then, really. Sometimes, the simplicity of stack overflow answers make me feel stupid. – guagay_wk Commented Nov 9, 2015 at 8:39
  • 1 @user781486 This question was helpful to me, and I used content from one of the answers inside my application. – steampowered Commented Jul 31, 2017 at 17:43
Add a ment  | 

3 Answers 3

Reset to default 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.

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./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.

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);

本文标签: javascriptRun function at set interval only at certain time of the dayStack Overflow