0

This is code used within nodered.
I'm invoking several timers with the same function, then either the timer actually runs and displays something, or I stop the timer (clearTimeout) and the something doens't get displayed.

The first thing I tried is this:

// multiple calls method 1 - DOES NOT WORK (multiple calls of procedure with same name - no method to distuinguish
function displaysomethingelse7 (rdelay7, var37, var47) {
    function performactualstuff (var3a7, var4a7) {
        node.warn ("37:"+var3a7+", 47:"+var4a7);
    }
    timer7=setTimeout(performactualstuff, rdelay7, var37, var47);
    node.warn ("starting timer27_inprocedure: "+timer7._idleStart);
    function stop7() {
        if (timer7) {
            clearTimeout(timer7);
            node.warn ("stopping timerid27 "+timer7._idleStart);
            timer7 = 0;
        }
    }
    return stop7;
}
// start 1
delay20=8500;
var20a=2;
var20b="b";
var t10 = displaysomethingelse7 (delay20, var20a, var20b);
// start 2
delay21=10500;
var21a=3;
var21b="c";
var t11 = displaysomethingelse7 (delay21, var21a, var21b);
// stop 1 ?
stopdelay30=8000;
setTimeout(t10, stopdelay30);
// stop 2 ?
stopdelay31=9000;
setTimeout(t11, stopdelay31);

This doens't work since the 'stop7' function has no method to disguinguish between timerIDs. So I came up with an array of functions:

// multiple calls method 2 - array of functions
function displaysomethingelsetoo (r2delay, var77, var88) {
    function performactualstufftoo (var77a, var88a) {
        node.warn ("77:"+var77a+", 88:"+var88a);
    }
    timer8=setTimeout(performactualstufftoo, r2delay, var77, var88);
    node.warn ("starting timer77_inprocedure= "+timer8._idleStart);
    if (typeof stopa  === 'undefined') stopa=[];
    stopa[timer8._idleStart] = function (tf) {
        if (tf) {
            clearTimeout(tf);
            node.warn ("stopping timerid3 "+tf._idleStart+"originaltimer="+timer8._idleStart);
            tf = 0;
        }
    }
    return stopa[timer8._idleStart];
}
// start 1
delay3=4000;
var5a=4;
var6a="d";
var t3a = displaysomethingelsetoo (delay3, var5a, var6a);
// start 2
delay4=5000;
var5b=5;
var6b="e";
var t3b = displaysomethingelsetoo (delay4, var5b, var6b);
// stop 1 ?
stopdelay3=2000;
setTimeout(t3a, stopdelay3, t3a);
// stop 2 ?
stopdelay4=3000;
setTimeout(t3b, stopdelay4, t3b);

But this isn't quite correct yet either - the stopa array has all the same function in it. I think the solution could be to pass the parsed timer8 variable to the stopa[timer8._idleStart] function, but I have no idea how to to do this.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Wannes
  • 41
  • 5
  • 3
    Your code would gain a lot readability if you'd start to give your variables names instead of numbers. – Bergi Dec 16 '15 at 10:03
  • how about you have a single start and stop function; the start function would return the timer handle which you can pass to the stop function - would that work for you? – Octav Zlatior Dec 16 '15 at 10:06

1 Answers1

1

This doens't work since the 'stop7' function has no method to disguinguish between timerIDs

You will want to use a closure here. I think you already tried to use one, and your code is structured like you were using one, there's only a tiny modification necessary: declare the variable as local to the displaysomethingelse7 function so that each invocation will create a new variable.

function displaysomethingelse(rdelay, a, b) {
    function performactualstuff() {
        node.warn ("37:"+a+", 47:"+b);
        // btw, you'll want to close over a and b here as well
    }
    var timer = setTimeout(performactualstuff, rdelay);
//  ^^^
    node.warn ("starting timer_inprocedure: "+timer._idleStart);
    return function stop() {
        if (timer) {
            clearTimeout(timer);
            node.warn ("stopping timer "+timer._idleStart);
            timer = 0;
        }
    };
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • thanks - that did the trick! (declaring the local variable) - and yes, it sounds like a good idea to have 1 start & 1 stop function... :) thx for the suggestions – Wannes Dec 16 '15 at 11:17