我正在使用 node-cron 来做某事。我编写的代码类似于 node-cron演示如下:
var cronJob = require('cron').CronJob;
var myJob = new cronJob('0 */10 * * * *', function(){
console.log(new Date().toLocaleString());
},function(){
},true);
console.log('now is ' + new Date().toLocaleString());
myJob.start();
我想在开始时立即开始工作,所以我将第三个参数设为 true。但它不起作用。我现在应该做什么来开始工作?
最佳答案
Iwant to start the job right now when i start, so the third args i make it true.
第四个参数,不是第三个*
myJob.start();
这条线是不必要的,并且可能会导致一些副作用。因为您已经通过将第四个参数设置为 true
来开始在构造函数中工作。
但是您显示的代码看起来有效。是什么让您认为它不起作用?您已将其设置为每 10 分钟运行一次,您是否等待时钟为 XX:X0:00 来查看代码?
为了进行测试,您可以将 '0 */10 * * * *'
更改为 '* * * * * *'
以查看代码是否无需等待即可运行。
关于node.js - 如何使用 node-cron 立即启 Action 业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24136237/