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. 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.
我正在尝试编写一个测试脚本,作为一个更大的项目的一部分,这将允许我安排未来的短信警报。在我的脚本中,我尝试使用节点包cron和node-schedule。我将日期对象设置为将来设置为三分钟。我使用这两个包来安排工作。但是,消息不会在正确的时间发送,而是立即发送。
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.
I used a fake number only for uploading to SO.
function twilSms.sendSms()在Node REPL中按预期工作。我只使用假号上传到SO。
Thanks for any help.
谢谢你的帮助。
1 个解决方案
#1
3
You should pass in a callable, e.g. an anonymous function as the second parameter to CronJob
constructor. 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). It doesn't matter that a function call is a parameter to another function, it executes immediately when the interpreter processes that line.
你应该传入一个可调用的,例如一个匿名函数作为CronJob构造函数的第二个参数。在你的代码中,你传递了twilSms.sendSms()的返回值,在这种情况下sendSms()立即执行(因为它最后被()调用)。函数调用是另一个函数的参数并不重要,它在解释器处理该行时立即执行。
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. E.g.
如果您在指定参数时不想使用匿名函数,则可以将函数分配给命名变量 - 但它仍然必须是CronJob期望的可调用函数。例如。
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. 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.
您在第二个注释中的示例似乎不正确,因为虽然sendSMS在这种情况下是可调用的,但它在运行时需要两个参数。 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, e.g.
如果希望能够在为CronJob()指定参数的同时将参数传递给sendSms(),则可以使用返回函数的函数,例如:
var sendSmsFunc = function(to, message) {
return function() {
twilSms.sendSms(to, message);
}
}
var cronSMS = new CronJob(inThreeMinutes, sendSmsFunc("123", "test message"), null, true);
#1
3
You should pass in a callable, e.g. an anonymous function as the second parameter to CronJob
constructor. 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). It doesn't matter that a function call is a parameter to another function, it executes immediately when the interpreter processes that line.
你应该传入一个可调用的,例如一个匿名函数作为CronJob构造函数的第二个参数。在你的代码中,你传递了twilSms.sendSms()的返回值,在这种情况下sendSms()立即执行(因为它最后被()调用)。函数调用是另一个函数的参数并不重要,它在解释器处理该行时立即执行。
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. E.g.
如果您在指定参数时不想使用匿名函数,则可以将函数分配给命名变量 - 但它仍然必须是CronJob期望的可调用函数。例如。
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. 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.
您在第二个注释中的示例似乎不正确,因为虽然sendSMS在这种情况下是可调用的,但它在运行时需要两个参数。 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, e.g.
如果希望能够在为CronJob()指定参数的同时将参数传递给sendSms(),则可以使用返回函数的函数,例如:
var sendSmsFunc = function(to, message) {
return function() {
twilSms.sendSms(to, message);
}
}
var cronSMS = new CronJob(inThreeMinutes, sendSmsFunc("123", "test message"), null, true);