我需要定期更新一个文件,该文件会给我其他计划任务。
我唯一的问题是使用cron-node
这里是测试代码(真实代码,仅限于 cron 问题,并带有一些测试行):
//Variable of the dayly update hour, with default value
var UPDATE_H=6,
UPDATE_M=30;
function start(){
console.log('start');
//test_
//Set the cron job at the next two minute following the start of the app
time=new Date();
time.setMinutes(time.getMinutes()+2);
UPDATE_H=time.getHours();
UPDATE_M=time.getMinutes();
//_test
//Start the cron job
smil_update(UPDATE_H, UPDATE_M);
//test_
//Print a dot every minutes
setInterval(function(){console.log('.');}, 60000);
//_test
player();
}
function smil_update(hour, min){
var cronJob=require('cron').CronJob,
when='',
//test_
time=new Date();
//_test
//Creating cron like date struct (cron like because seconds count too here)
when='00 '+min+' '+hour+' * * *';
console.log('Will work at:'+when);
//test_
console.log('It is '+time.getHours()+' '+time.getMinutes());
//_test
var job=new cronJob(when, timeZone='Europe/Paris', function(){
console.log('UPDATE');
player();
});
job.start();
}
function player(){
console.log('Player');
}
start();
我得到:
start
Will work at:00 13 17 * * *
It is 17 11
Player
.
.
.
似乎我在某个地方错过了一点,但即使在重新阅读文档之后,我也没有找到我做错的地方。