仅在一天中的特定时间以设定的间隔运行功能 [英] Run function at set interval only at certain time of the day

查看:63
本文介绍了仅在一天中的特定时间以设定的间隔运行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在全天候定期运行一个函数.

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

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

不幸的是,这不是我想要的.我希望此功能仅在从早上 0900 小时到 1800 小时的固定时间间隔内运行.该功能不应在这些时间之外运行.这如何在 node.js 中完成?是否有方便使用的模块或函数?

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?

推荐答案

您可以简单地检查当前时间是否在所需的时间范围内,然后使用它来决定是否执行您的函数.

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

这将在 9:00 到 18:00 之间每分钟运行一次您的函数.

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

这篇关于仅在一天中的特定时间以设定的间隔运行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆