简体   繁体   English

JavaScript:节点cron和节点计划作业立即执行,而不是将来执行

[英]JavaScript: Node cron and node-schedule jobs execute immediately instead of in the future

I am attempting to write a test-script, as part of a larger project which will allow me to schedule future text message alerts. 我试图编写一个测试脚本,作为一个较大的项目的一部分,这将使我能够计划将来的文本消息警报。 In my script, I try to use both node packages cron and node-schedule. 在我的脚本中,我尝试同时使用节点程序包cron和node-schedule。 I configure a date object set to three minutes in the future. 我将日期对象配置为将来的三分钟。 I use both packages to schedule the job. 我使用这两个软件包来安排工作。 However, the messages are not sent at the right time, but are sent immediately. 但是,邮件不会在正确的时间发送,而是立即发送。

I want my code to execute at a set time in the future, does anyone know why the following code would not work? 我希望我的代码在将来的指定时间执行,有人知道为什么下面的代码不起作用吗?

var CronJob = require('cron').CronJob;
var twilSms = require('./TwilSms');
var schedule = require('node-schedule');

var sendInThree = function(to,message) {
    var threeMinutes = 180000;
    var inThreeMinutes = new Date(Date.now()+threeMinutes);
    process.stdout.write("A text message should be sent at: \n" + inThreeMinutes.toString() + '\n');

    //TRIGGERS INSTANTLY
    var sched = schedule.scheduleJob(inThreeMinutes,twilSms.sendSms(to,
                   ('Did this message arrive at: ' + inThreeMinutes.toString() + '?')));


    //TRIGGERS INSTANTLY
    var cronSMS = new CronJob(inThreeMinutes,
                twilSms.sendSms(to,
                    ('Did this message arrive at: ' + inThreeMinutes.toString() + '?')),
                null, true);
}


sendInThree('13125555555');

NOTES: 笔记:

function twilSms.sendSms() workes as expected in the Node REPL. 函数twilSms.sendSms()在节点REPL中按预期工作。
I used a fake number only for uploading to SO. 我仅使用伪造的号码上传到SO。

Thanks for any help. 谢谢你的帮助。

You should pass in a callable, eg an anonymous function as the second parameter to CronJob constructor. 您应该将可调用的(例如,匿名函数)作为第二个参数传递给CronJob构造函数。 In your code, you are passing in the return value of twilSms.sendSms() in which case sendSms() executes immediately (because it's called by the () at the end). 在您的代码中,您传入了twilSms.sendSms()的返回值,在这种情况下, sendSms()立即执行(因为它由()最后调用)。 It doesn't matter that a function call is a parameter to another function, it executes immediately when the interpreter processes that line. 一个函数调用是另一个函数的参数无关紧要,它在解释器处理该行时立即执行。

var cronSMS = new CronJob(inThreeMinutes, function() {
                twilSms.sendSms(to,
                    ('Did this message arrive at: ' + inThreeMinutes.toString() + '?')) },
                null, true);

Update 1 更新1

If you don't want to use an anonymous function when specifying the parameters, you can assign a function to a named variable - but it still has to be a callable as CronJob expects it to be. 如果您不想在指定参数时使用匿名函数,则可以将函数分配给命名变量-但它仍必须像CronJob期望的那样是可调用的。 Eg 例如

var sendSmsFunc = function() {
    twilSms.sendSms(to,
         ('Did this message arrive at: ' + inThreeMinutes.toString() + '?'))
}

var cronSMS = new CronJob(inThreeMinutes, sendSmsFunc, null, true);

The example you have in your 2nd comment doesn't seem right, because although sendSMS is a callable in that case, it expects two parameters at runtime. 第二个注释中的示例似乎并不正确,因为尽管在这种情况下sendSMS是可调用的,但它在运行时需要两个参数。 CronJob will not pass any parameters so it must be a callable that doesn't expect parameters just simply wants to be called (or executed) at runtime to do its job. CronJob不会传递任何参数,因此它必须是可调用的,不会期望仅在运行时调用(或执行)参数来完成其工作。

Update 2 更新2

If you want to be able to pass in parameters to sendSms() at the same time when specifying parameters to CronJob() , you can use a function that returns a function, eg 如果希望在向sendSms()指定参数时同时将参数传递给CronJob() ,则可以使用返回函数的函数,例如

var sendSmsFunc = function(to, message) {
    return function() {
        twilSms.sendSms(to, message);
    }
}

var cronSMS = new CronJob(inThreeMinutes, sendSmsFunc("123", "test message"), null, true);

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

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