节点计划不起作用

时间:2014-11-29 07:55:22

标签: node.js

我想使用node-schedule,我每天从Data Base获取信息,并且我想在特殊时间做一些事情。 这是我的代码:

users.forEach(function(users_entry){
                                if(err){
                                    console.log(err);
                                }
                                else{
                                   var date = new Date(2014, 11, 29, 11, 45, 0);
                                   schedule.scheduleJob(date,
                                   function(){
                                      console.log('The world is going to end today.');
                                   });
                                 }
                          });

但是上面的代码并没有在上述时间运行并且一直有效。 什么是问题?

3 个答案:

答案 0 :(得分:3)

我改变了我的代码并使用了cron。 https://www.npmjs.org/package/cron

效果很好:))

var CronJob = require('cron').CronJob;
new CronJob('* * * * * *', function(){
    console.log('You will see this message every second');
}, null, true, "America/Los_Angeles");

答案 1 :(得分:0)

试试这个

var CronJob = require('cron').CronJob;
var job = new CronJob('00 00 12 * * 1-7', function() {
/*
 * Runs every day
 * at 12:00:00 AM.
*/
}, function () {
 /* This function is executed when the job stops */
},
 true, /* Start the job right now */
 timeZone /* Time zone of this job. */
);

答案 2 :(得分:0)

试试这个:

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

const rule = scheduled.RecurrenceRule();

let time = '2021-07-30 14:20:00';
rule.tz = 'Asia/Kolkata';
rule.year = moment(time).year();
rule.month = moment(time).month();
rule.date = moment(time).date();
rule.hour = moment(time).hours();
rule.minute = moment(time).minutes();
rule.second = moment(time).seconds();
schedule.scheduleJob(rule, async function () {
     console.log("Scheduled")
});