Hello.
I would like to make a countdown timer using a function node.
Counter needs to be reset to its initial value when the function is triggered, so as long the function is triggered before the counter value reaches 0 it will never timeout.
I will need also the possibility to stop it.
My idea was to clearTimeout every time the function node is triggered and then reset it back to the value I want so my countdown function will be executed when there is no trigger.
I'm aware of the various countdown and stop timer nodes, but I don't want to use them as they either cannot be set dynamically or output stuff I don't need.
My function:
clearTimeout(countdown); //this should clear the Timeout
setTimeout(countdown, 3000); //this should trigger function when no trigger recieved
context.set(['target', 'current'] , [5000, 0]);
var target = context.get('target'); // 5s
var current = context.get('current'); // 0 secs
function countdown() {
current = context.get('current') + 1000;
context.set('current', current);
let diff = target-current;
let sec = (diff/1000); // gets secs
if (diff > 0 ) {
setTimeout(countdown, 1000);
msg.timeout = false;
msg.sec = sec;
node.send(msg);
}
else if (diff === 0 ) {
msg.sec = 0;
msg.timeout = true;
node.send(msg);
}
}
return;
This is my test flow:
[{"id":"2c1af103.8bdcce","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"4c1e07a9.8e7ac8","type":"inject","z":"2c1af103.8bdcce","name":"Trigger","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":250,"y":120,"wires":[["e97d043.a3fa6f8"]]},{"id":"5baef51.34a8d0c","type":"debug","z":"2c1af103.8bdcce","name":"true","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":750,"y":100,"wires":[]},{"id":"e97d043.a3fa6f8","type":"function","z":"2c1af103.8bdcce","name":"","func":"clearTimeout(countdown); //this should clear the Timeout \nsetTimeout(countdown, 3000); //this should trigger function when no trigger recieved\n//setTimeout(() => countdown(), 3000);\ncontext.set(['target', 'current'] , [5000, 0]);\nvar target = context.get('target'); // 5s\nvar current = context.get('current'); // 0 secs\n\nfunction countdown() {\n current = context.get('current') + 1000;\n context.set('current', current);\n let diff = target-current;\n let sec = (diff/1000); // gets secs\n if (diff > 0 ) {\n setTimeout(countdown, 1000);\n msg.timeout = false;\n msg.sec = sec;\n node.send(msg);\n }\n else if (diff === 0 ) {\n msg.sec = 0;\n msg.timeout = true;\n node.send(msg);\n }\n}\nreturn;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":430,"y":120,"wires":[["8f67b9a2.77a558"]]},{"id":"8f67b9a2.77a558","type":"switch","z":"2c1af103.8bdcce","name":"","property":"timeout","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":600,"y":120,"wires":[["5baef51.34a8d0c"],["69eabb27.9e9834"]]},{"id":"69eabb27.9e9834","type":"debug","z":"2c1af103.8bdcce","name":"false","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":750,"y":140,"wires":[]}]
I will appreciate any ideas on how to approach this.