Thank you Trying_to_learn & Colin.
I get closer in understanding, but after many hours of searching it is still a mystery to me why it is not working.
The Function Node is now listening to the topic 1 & 0 input.
But once started with 1 it keeps counting to 5 and than stops.
If i first inject a 1 the countdown starts.
If 0 is injected while running it just shows 10 in between. But the original counting keeps going down.
I don't understand why:
- The clearInterval from "if (counter <5) " stops the Interval.
- The one from "if (msg.topic == '0')" does only restart an separate IF process putting out the number 10.
But does not stop the countdown of the still running Interval.
var counter = 10;
var countdown = setInterval(function(){
node.send({payload:(counter)});
if (msg.topic == '1') {
counter--;
}
if (counter <5) {
clearInterval(countdown);
}
if (msg.topic == '0') {
clearInterval(countdown);
}
}, 1000)
Here the nodes exported:
[{"id":"9a551ca4.5b30b","type":"function","z":"d2d9018a.7d512","name":"Countdown","func":"var counter = 10;\n\nvar countdown = setInterval(function(){\n\n node.send({payload:(counter)});\n node.status({fill:\"green\",shape:\"ring\",text:(counter)});\n\n if (msg.topic == '1') {\n counter--;\n }\n \n if (counter <5) {\n clearInterval(countdown);\n node.status({fill:\"red\",shape:\"dot\",text:'finished'});\n }\n\n if (msg.topic == '0') {\n clearInterval(countdown);\n node.status({fill:\"red\",shape:\"dot\",text:'stopped'});\n \n }\n}, 1000)","outputs":1,"noerr":0,"initialize":"","finalize":"","x":350,"y":120,"wires":[["4400ea00.ee4d34"]],"icon":"font-awesome/fa-hourglass-1"},{"id":"913ccad1.53a7e8","type":"inject","z":"d2d9018a.7d512","name":"1","props":[{"p":"topic","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","x":180,"y":120,"wires":[["9a551ca4.5b30b"]],"icon":"node-red/arrow-in.svg"},{"id":"4400ea00.ee4d34","type":"debug","z":"d2d9018a.7d512","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":120,"wires":[]},{"id":"b9f004b9.08bc68","type":"inject","z":"d2d9018a.7d512","name":"0","props":[{"p":"topic","v":"0","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","x":170,"y":180,"wires":[["9a551ca4.5b30b"]],"icon":"node-red/arrow-in.svg"}]