# clearTimeout not function

**URL:** <https://discourse.nodered.org/t/cleartimeout-not-function/90870>\
**Category:** General\
**Tags:** function-node\
**Created:** [10 September 2024 19:50 UTC](https://discourse.nodered.org/t/cleartimeout-not-function/90870 "2024-09-10T19:50:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![wilson2517](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wilson2517/32/94347_2.png) [@wilson2517](https://discourse.nodered.org/u/wilson2517)\
**Post date:** [10 September 2024 19:50 UTC](https://discourse.nodered.org/t/cleartimeout-not-function/90870/1 "2024-09-10T19:50:56Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [10 September 2024 19:58 UTC](https://discourse.nodered.org/t/cleartimeout-not-function/90870/2 "2024-09-10T19:58:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [10 September 2024 20:02 UTC](https://discourse.nodered.org/t/cleartimeout-not-function/90870/3 "2024-09-10T20:02:27Z")

</div>

> [@Steve-Mcl](#):
>
> You _can_ do this with context (to store the timer handle)

Like this @wilson2517 ...

```auto
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
}

```

---

<div class="post-metadata">

**Author:** ![wilson2517](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wilson2517/32/94347_2.png) [@wilson2517](https://discourse.nodered.org/u/wilson2517)\
**Post date:** [10 September 2024 20:07 UTC](https://discourse.nodered.org/t/cleartimeout-not-function/90870/4 "2024-09-10T20:07:18Z")

</div>

> [@Steve-Mcl](#):
>
> 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.

Wow!!!! I'm learning THX

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [10 September 2024 20:24 UTC](https://discourse.nodered.org/t/cleartimeout-not-function/90870/5 "2024-09-10T20:24:38Z")

</div>

> [@Steve-Mcl](#):
>
> There is a delay node built-in to node-red.

In case you were wondering @wilson2517 ...

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![wilson2517](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wilson2517/32/94347_2.png) [@wilson2517](https://discourse.nodered.org/u/wilson2517)\
**Post date:** [10 September 2024 23:55 UTC](https://discourse.nodered.org/t/cleartimeout-not-function/90870/6 "2024-09-10T23:55:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 September 2024 07:40 UTC](https://discourse.nodered.org/t/cleartimeout-not-function/90870/7 "2024-09-11T07:40:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wilson2517](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wilson2517/32/94347_2.png) [@wilson2517](https://discourse.nodered.org/u/wilson2517)\
**Post date:** [11 September 2024 22:02 UTC](https://discourse.nodered.org/t/cleartimeout-not-function/90870/8 "2024-09-11T22:02:37Z")

</div>

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

 ![delay](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/2/c2ba62535e2e38a7496e94a7149fb0328661c349.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 September 2024 22:03 UTC](https://discourse.nodered.org/t/cleartimeout-not-function/90870/9 "2024-09-25T22:03:04Z")

</div>

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