启动和停止计时器:对多个实例使用 1 个函数

starting & stopping timers: using 1 function for multiple instances

这是 nodered 中使用的代码。
我正在调用几个具有相同功能的计时器,然后计时器实际运行并显示一些东西, 或者我停止计时器 (clearTimeout) 并且没有显示某些内容。

我尝试的第一件事是:

// 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);

这不起作用,因为 'stop7' 函数无法区分 timerID。 所以我想出了一个函数数组:

// 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);

但这也不完全正确 - stopa 数组在其中具有所有相同的功能。 我认为解决方案可能是将解析后的 timer8 变量传递给 stopa[timer8._idleStart] 函数, 但我不知道该怎么做。

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

您需要使用 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 作为 displaysomethingelse7 函数的局部变量,这样每次调用都会创建一个新变量。

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;
        }
    };
}