# setTimeout - clearTimeout

**URL:** <https://discourse.nodered.org/t/settimeout-cleartimeout/38515>\
**Category:** General\
**Created:** [4 January 2021 12:22 UTC](https://discourse.nodered.org/t/settimeout-cleartimeout/38515 "2021-01-04T12:22:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [4 January 2021 12:22 UTC](https://discourse.nodered.org/t/settimeout-cleartimeout/38515/1 "2021-01-04T12:22:37Z")

</div>

I am trying to understand how setTimeout and clearTimeout works.  
Please have a look at the attached flow.  
Normal delay of 5 sec works ok. But when I send a reset the output shows the time of the reset but at the end of 5 seconds I have another output. How can I avoid the last output.  
It seems that clearTimeout does not kill the original timeout.

```auto
[{"id":"b3db555d.238d78","type":"tab","label":"timeout","disabled":false,"info":""},{"id":"d5068c.22d52978","type":"function","z":"b3db555d.238d78","name":"delay ","func":"let delay = 5000;\nlet t;\n\n//------------------------\n\nfunction myDelayFunction() {\n t = setTimeout(myEndFunction, delay);\n}\n\n\nfunction myEndFunction() {\n node.send({payload:Date.now()});\n}\n\n\nfunction myResetFunction() {\n clearTimeout(t);\n}\n\n//------------------------\n\nnode.send({payload:Date.now()});\nmyDelayFunction();\n\nif (msg.reset){\n myResetFunction();\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":310,"y":120,"wires":[["98c94581.c06f38"]]},{"id":"5cdf28a5.19ca08","type":"inject","z":"b3db555d.238d78","name":"5 sec","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":170,"y":100,"wires":[["d5068c.22d52978"]]},{"id":"98c94581.c06f38","type":"debug","z":"b3db555d.238d78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":120,"wires":[]},{"id":"13cd5b94.2603b4","type":"inject","z":"b3db555d.238d78","name":"reset","props":[{"p":"reset","v":"0","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":170,"y":140,"wires":[["d5068c.22d52978"]]}]

```

---

<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:** [4 January 2021 12:32 UTC](https://discourse.nodered.org/t/settimeout-cleartimeout/38515/2 "2021-01-04T12:32:37Z")

</div>

It doesn't work because you are not fully understanding how node-red works. Each time a msg hits that function, all code in the function is executed (including `let t`). So when you try to `clearTimeout(t);`, `t` is empty!

If you want this to work, you will need to store and restore the timer handle `t`. See "working with context" in the node-red documentation to understand how you can persist a value between calls.

However, why not use a delay node instead...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/3/630973b0943d40d4def24339f1049dd1d7849e29.png)

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [4 January 2021 13:01 UTC](https://discourse.nodered.org/t/settimeout-cleartimeout/38515/3 "2021-01-04T13:01:45Z")

</div>

yes, I now that I can use the delay node but I wanted to understand how setTimeout and clearTimeout works in node-red.  
Many thanks for your explanation.

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [4 January 2021 13:20 UTC](https://discourse.nodered.org/t/settimeout-cleartimeout/38515/4 "2021-01-04T13:20:01Z")

</div>

now it works with multi units (ms, sec, min, std, day in msg.unit) 🙂

```auto
[{"id":"9ba032fb.5b15","type":"function","z":"b3db555d.238d78","name":"multi unit delay","func":"let delay = 0;\n\n//==================================\n\nfunction myDelayFunction() {\n let t = setTimeout(myEndFunction, delay);\n context.set('tout',t);\n}\n\n\nfunction myEndFunction() {\n node.send({payload:Date.now()});\n node.send({payload:msg.payload}); \n}\n\n\nfunction myResetFunction() {\n let t = context.get('tout'); \n clearTimeout(t);\n node.send({payload:Date.now()}); \n node.send({payload:context.get('pl'),delay:\"reset\"}); \n}\n\n//==================================\n\nif (msg.reset){\n myResetFunction();\n}\nelse{\n context.set('pl',msg.payload); \n switch(msg.unit) {\n case \"ms\":\n delay = msg.delay;\n break;\n case \"sec\":\n delay = msg.delay * 1000;\n break;\n case \"min\":\n delay = msg.delay * 1000*60;\n break; \n case \"std\":\n delay = msg.delay * 1000*60*60;\n break;\n case \"day\":\n delay = msg.delay * 1000*60*60*24;\n break; \n default:\n return null;\n}\n node.send({payload:Date.now()});\n myDelayFunction(); \n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":540,"wires":[["92b19abc.1dd9a8"]]},{"id":"54c98a44.4a8724","type":"inject","z":"b3db555d.238d78","name":"5 min","props":[{"p":"payload"},{"p":"delay","v":"1","vt":"num"},{"p":"unit","v":"min","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Test","payloadType":"str","x":150,"y":520,"wires":[["9ba032fb.5b15"]]},{"id":"92b19abc.1dd9a8","type":"debug","z":"b3db555d.238d78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":470,"y":540,"wires":[]},{"id":"764b5560.f001ac","type":"inject","z":"b3db555d.238d78","name":"reset","props":[{"p":"reset","v":"0","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":150,"y":560,"wires":[["9ba032fb.5b15"]]}]

```

---

<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:** [18 January 2021 13:20 UTC](https://discourse.nodered.org/t/settimeout-cleartimeout/38515/5 "2021-01-18T13:20:03Z")

</div>

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