Ad

Node Schedule Not Working With Object Variable?

- 1 answer

I have programmed a schedule in node:

function setTimer(time){
  t_SD1.on_h = time.on_h;
  t_SD1.on_m = time.on_m;
  t_SD1.off_h = time.off_h;
  t_SD1.off_m = time.off_m;
   console.log(t_SD1.on_h+' '+t_SD1.on_m);
}
var j = schedule.scheduleJob({hour: t_SD1.on_h, minute: t_SD1.on_m}, function(){
  console.log('ONNNN');
  console.log('0 '+t_SD1.on_m+' '+t_SD1.on_h+' * * *');
});

The time comes from:

var obj = JSON.parse(req.query.timer);


setTimer(obj);

Declaration:

var t_SD1 = {on_h: 14, on_m: 52, off_h: 14, off_m: 53};

Ok... now the problem:

When I start it without setTimer() it works fine. The schedule triggers at 14:52. But when I start the setTimer() function it does not work.

My console tells me the time 0 53 14 * * *

I have also tried it with parseInt(time.on_h), but the same result.

Ad

Answer

It's a bit hard to understand under which conditions it does and doesn't fail, but I think the problem stems from the fact that you call scheduleJob() before setTimer(). I think you need to adjust your first example to this:

function setTimer(time){
  t_SD1.on_h = time.on_h;
  t_SD1.on_m = time.on_m;
  t_SD1.off_h = time.off_h;
  t_SD1.off_m = time.off_m;
   console.log(t_SD1.on_h+' '+t_SD1.on_m);

  var j = schedule.scheduleJob({hour: t_SD1.on_h, minute: t_SD1.on_m}, function(){
    console.log('ONNNN');
    console.log('0 '+t_SD1.on_m+' '+t_SD1.on_h+' * * *');
  });
}
Ad
source: stackoverflow.com
Ad