使用cron-node,我哪里出错了?

时间:2013-09-11 15:12:55

标签: javascript node.js

我需要定期更新文件,该文件将为我提供其他计划任务。

我唯一的问题是使用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
.
.
.

似乎我在某个地方错过了一点,但即使重读了文档,我也无法得到我做错的地方。

1 个答案:

答案 0 :(得分:0)

我明白了。

错误在于变量。

而不是

when='00 '+...

一定是

when='0 '+...

似乎cron不认识00为0,原因不明。