setTimeout - clearTimeout

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.

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

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

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.

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

[{"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"]]}]
1 Like

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