Hi, I'm new in node red and I confused when I try to make some simple timer to count for a total time when the true value injected here's the code I write
var counterLStime = flow.get("counter")||0;
var trigger = msg.payload;
var time = setInterval(getcounter, 1000);
var count = 0;
function getcounter() {
if (trigger == false) {
clearInterval(time);
flow.set("counter", 0)
}
else if (trigger == true) {
++counterLStime;
flow.set("counter", counterLStime)
}
}
msg.payload = {
"Total Time": counterLStime
}
return msg;
and flow I make
[
{
"id": "0c3e6e4c03fba675",
"type": "function",
"z": "6c67e417f0d6a0e7",
"name": "function 21",
"func": "var counterLStime = flow.get(\"counter\")||0;\nvar trigger = msg.payload;\nvar time = setInterval(getcounter, 1000);\nvar count = 0;\n\nfunction getcounter() {\n if (trigger == false) {\n clearInterval(time);\n flow.set(\"counter\", 0)\n }\n else if (trigger == true) {\n ++counterLStime;\n flow.set(\"counter\", counterLStime)\n }\n}\n\nmsg.payload = {\n \"Total Time\": counterLStime\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 910,
"y": 420,
"wires": [
[
"9d65ca5725ee1baf"
]
]
},
{
"id": "48b35364becfe3f6",
"type": "inject",
"z": "6c67e417f0d6a0e7",
"name": "",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "true",
"payloadType": "bool",
"x": 710,
"y": 400,
"wires": [
[
"0c3e6e4c03fba675"
]
]
},
{
"id": "b0fca08d5c4b4469",
"type": "inject",
"z": "6c67e417f0d6a0e7",
"name": "",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "false",
"payloadType": "bool",
"x": 710,
"y": 440,
"wires": [
[
"0c3e6e4c03fba675"
]
]
},
{
"id": "9d65ca5725ee1baf",
"type": "debug",
"z": "6c67e417f0d6a0e7",
"name": "debug 134",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 1090,
"y": 420,
"wires": []
}
]
Maybe can you help me what wrong with this code because the value are showed in debug but when I pressed false the value still counting and not reset to 0.