简体   繁体   English

请帮忙,我不知道如何在 node.js 中使用节点调度模块

[英]Please help, I can't figure out how to use node-schedule module in node.js

I'm new to js and programming in general, so I'm sorry for this dumb question, but I need to have separated scheduled jobs in my script using node-schedule, but I can't figure out how because each time when 1st scheduled job is to be executed it immediately executes both the first and the second job.我是 js 和一般编程的新手,所以我很抱歉这个愚蠢的问题,但我需要在我的脚本中使用 node-schedule 分离预定的作业,但我不知道怎么做,因为每次第一次计划的作业将被执行,它立即执行第一个和第二个作业。 And the functions even get executed the other way around, the second function gets executed first (which is set to a later time than first function) and the first function gets executed second.并且这些函数甚至以相反的方式执行,第二个 function 首先执行(设置为比第一个函数晚),第一个 function 第二个执行。

My code:我的代码:

const schedule = require('node-schedule');

//first job
let firstPost = new Date('2022-01-08T06:30:00.000+4:00');

schedule.scheduleJob(firstPost, function () {
    console.log('first job');
});

// second job
let secondPost = new Date('2022-01-08T06:32:00.000+4:00');

schedule.scheduleJob(secondPost, function () {
    console.log('second job');
 
});

node-schedule take two arguments. node-schedule 取两个 arguments。 1st one being the time(in cron or equivalent syntax.) 2nd argument being the callback function which will be triggered at the time syntax match.第一个是时间(在 cron 或等效语法中)。第二个参数是回调 function,它将在时间语法匹配时触发。

check the Date format.检查日期格式。

node> new Date('2022-01-08T06:30:00.000+4:00')
Invalid Date

Examples with the cron format: cron 格式的示例:

const schedule = require('node-schedule');
const date = new Date(2012, 11, 21, 5, 30, 0);

const job = schedule.scheduleJob(date, function(){
  console.log('The world is going to end today.');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM