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