clearTimeout not function

Hi, I'm new here, I'm trying to use clearTimeout, but it doesn't work, I guess I'm doing something wrong.
This is my code

var entrada = msg.payload;
var testset;

if(entrada == "on"){

    testset = setTimeout(function() {

        msg.payload = "active";
        node.send(msg);
        
    }, 2000);

}
if (entrada == "off"){

    clearTimeout(testset);

}

I would greatly appreciate any help in learning.

Every time a function runs, everything is executed from top to bottom. Variables do not persist.

You can do this with context (to store the timer handle) but the question is why??? There is a delay node built-in to node-red.

Like this @wilson2517 ...

var entrada = msg.payload;
var testset = context.get("testset");  // Retrieve the stored timeout reference

if (entrada == "on") {
    testset = setTimeout(function() {
        msg.payload = "active";
        node.send(msg);
    }, 2000);

    context.set("testset", testset);  // Store the timeout reference for future use
}

if (entrada == "off") {
    clearTimeout(testset);  // Clear the timeout
    context.set("testset", null);  // Clear the stored timeout reference
}
2 Likes

Wow!!!! I'm learning THX

In case you were wondering @wilson2517 ...

[{"id":"6214978a5e43666b","type":"switch","z":"be4568a984cb4685","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":430,"y":2100,"wires":[["0ff3f2b1da3f439e"],["92b4608997303baf"]]},{"id":"92b4608997303baf","type":"debug","z":"be4568a984cb4685","name":"debug 2482","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":2100,"wires":[]},{"id":"0ff3f2b1da3f439e","type":"delay","z":"be4568a984cb4685","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":560,"y":2080,"wires":[["92b4608997303baf"]]},{"id":"bf5f509eb2d579fb","type":"inject","z":"be4568a984cb4685","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":250,"y":2080,"wires":[["6214978a5e43666b"]]},{"id":"8378a2e9f3699002","type":"inject","z":"be4568a984cb4685","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":250,"y":2120,"wires":[["6214978a5e43666b"]]}]

yes, I know, but clearTimeout stop fire command of setTimeout, delay is for other thing.

The point is that you don't need to use setTimeout at all. You can use a Delay node instead.

Yes I know, setTimeout and clearTimeout is for specific use, delay is normal for me.

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