admin管理员组文章数量:1399898
Using node-scheduler to set up recurring job. I need 7 days a week at several specific times - e.g. 5:45am, 06:30am, 11:15am, etc.
I set up rule:
let rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [1,2,3,4,5,6,7]
Now if I add hours and minutes as arrays it looks like they run through all hours for all minutes - e.g.
rule.hour = [5,6,11];
rule.minute = [45,30,15];
So, this will run at 5:45am, 6:45am, 11:45am, 5:30am, 6:30am, 11:30am, ....
How can I use rules to set up for exact times? Again, in my example 5:45am, 06:30am, 11:15am
Using node-scheduler to set up recurring job. I need 7 days a week at several specific times - e.g. 5:45am, 06:30am, 11:15am, etc.
I set up rule:
let rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [1,2,3,4,5,6,7]
Now if I add hours and minutes as arrays it looks like they run through all hours for all minutes - e.g.
rule.hour = [5,6,11];
rule.minute = [45,30,15];
So, this will run at 5:45am, 6:45am, 11:45am, 5:30am, 6:30am, 11:30am, ....
How can I use rules to set up for exact times? Again, in my example 5:45am, 06:30am, 11:15am
Share Improve this question asked Mar 29, 2019 at 23:29 rfossellarfossella 1271 gold badge3 silver badges11 bronze badges2 Answers
Reset to default 4Another approach is to use Object Literal Syntax. If the weekday is not specified as shown below the times apply for being scheduled each day.
let times = [
{hour: 5, minute: 45},
{hour: 6, minute: 30},
{hour: 11, minute: 15}
];
times.forEach(function(time) {
var j = schedule.scheduleJob(time, function() {
// your job
console.log('Time for tea!');
});
})
The node-schedule
module seems to support cron syntax. But as the schedule you want, cannot be expressed in a single rule with cron, you also can't define that in a single rule in node-schedule. Using the dayOfWeek
, hour
and minute
definitions for the rule schedules the job on every bination of day
, hour
and minute
, like a cron rule does.
Have a look at the recurrence rule scheduling section in the docs, how to define a rule for a specific time every day, and create and schedule a separate rule for each time you need
let hour = [5, 6, 11];
let minute = [45, 30, 15];
for (let i = 0; i < hour.length; i++) {
let rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [0, 1, 2, 3, 4, 5, 6];
rule.hour = hour[i];
rule.minute = minute[i];
let j = schedule.scheduleJob(rule, function(){
//your job
});
}
本文标签: javascriptUsing nodeschedule to run at *specific* timesStack Overflow
版权声明:本文标题:javascript - Using node-schedule to run at *specific* times - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744223673a2595989.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论