Javascript clearInterval in if statement

I have a function where I want to use setInterval to start a timer and using if statements, do specific commands. I noticed that multiple timer intervals are being created. From what I can tell the new timers are starting when the inject node kicks off on it's next interval. Hopefully some one can point me in the right direction.

var radis = global.get("radios");  //comes from FRStack
var cli = global.get("clients"); //comes from FRStack
var radioStatus = global.get("radioStatus");
var clientStatus = global.get("clientStatus");
var timeout = 30;
var desc; // function description for debugging 

if (radis && radis.length !== 0) { //Radio On 
    global.set('radioStatus', 1);
if (radis && radis.length === 0) { //Radio Off
    global.set('radioStatus', 0);
if (cli && cli.length !== 0) { //Client On
    global.set('clientStatus', 1);
if (cli && cli.length === 0) {  //Client Off
    global.set('clientStatus', 0);

var countdown = setInterval(function () {
    if (clientStatus === 1 && radioStatus === 1)  {     
       desc = " Radio/Client On; timer reset to ";
       node.send({payload: desc + timeout})
    if (clientStatus === 0 && radioStatus === 1 && timeout > 1) {
        desc = "Radio On; No Client; Shutting Radio Down in ";
        node.send({ payload: desc + timeout })
    if (isNaN(timeout)) {
        desc = "Radio On; No Client; Shutting Radio Down ";
        node.send({ payload: 0})
    node.status({ text: (desc + timeout + " seconds") }); //used for debug
}, 1000);  //counter interval

Each time you get a message coming in it calls setInterval which creates a new interval timer, so unless each one gets cleared at some point then you will end up with multiple running.

Also note that the variables you create at the beginning, such as radioStatus are effectively local to each entry into the function node. In your timer function, for any particular timer (of which you create multiple) those variables will never change, so if the first test does not pass the first time then it will never the timer will never be cleared. If you want them to react to changes in the globals then pick them up each time round the function loop. Alternatively perhaps there is just an error in the code and you want it to clear the timer when the timeout runs down whatever is the current status.
I don't understand wht the isNaN test is for, I don't see how timeout cannot be a valid number.

You need to keep a reference to the interval. That reference needs to be stored in a node-red context variable so that it survives between calls to the function node.

So you should try to retrieve the context variable at the start of the function and if it exists and isn't undefined then use it to cancel the existing interval.

Thanks, I agree I need to be able to set and clear the interval timer based on a context variable. To answer Colin regarding isNAN I watched the timer count negative integers and read up on isNAN and thought it would be able to detect when I get down to then end of the timer. Probably easier just to tell the time to clear on zero.


Use <= rather than == just in case it ever ends up negative. That is Defensive Programming. It reduces the chance of it going horribly wrong if something unexpected happens.

Yeah that is what I had originally. Even with the multiple intervals it worked however I am just trying to code it correctly...

Thanks all

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.