我正在尝试用node-red编码一个功能节点,当从注入节点获取in_array( $userRole, $location['c']
为true时,输出为每秒递增1,而当获取msg.paylaod
时停止,输出为每秒递增1来自另一个注入节点。
它开始提供输出,但是在注入有效载荷“ false”时不会停止。
节点代码:
msg.payload
功能节点代码:
[
{
"id":"b6c9b219.90a478",
"type":"function",
"z":"a3d6aff.bd4935",
"name":"",
"func":"var i = 1;\nfunction increment(){\n i = i + 1;\n msg={payload:i};\n node.send(msg);\n \n}\nif(msg.payload===true){\nvar interval = setInterval( increment, 1000);\nif(msg.payload===false){\n clearInterval(interval);\n}\n}\n\n",
"outputs":1,
"noerr":0,
"x":700.5,
"y":581,
"wires":[
[
"12f2090c.587347"
]
]
},
{
"id":"12f2090c.587347",
"type":"debug",
"z":"a3d6aff.bd4935",
"name":"",
"active":true,
"tosidebar":true,
"console":false,
"tostatus":false,
"complete":"true",
"x":826.5,
"y":473,
"wires":[
]
},
{
"id":"26abbbf.05cf944",
"type":"inject",
"z":"a3d6aff.bd4935",
"name":"",
"topic":"",
"payload":"true",
"payloadType":"bool",
"repeat":"",
"crontab":"",
"once":false,
"onceDelay":0.1,
"x":654.88330078125,
"y":767.2833251953125,
"wires":[
[
"b6c9b219.90a478"
]
]
},
{
"id":"880341a0.dcab2",
"type":"inject",
"z":"a3d6aff.bd4935",
"name":"",
"topic":"",
"payload":"false",
"payloadType":"bool",
"repeat":"",
"crontab":"",
"once":false,
"onceDelay":0.1,
"x":643.88330078125,
"y":824.0999755859375,
"wires":[
[
"b6c9b219.90a478"
]
]
}
]
答案 0 :(得分:0)
最简单的答案是您不会(轻松)。
执行此操作的更好方法是使函数使用发送multiple messages的格式一次发送序列中的所有消息。然后使用延迟节点对流进行速率限制,以使消息每秒释放一次。
如果您真正需要(确实不需要)按照自己的方式进行操作,则需要将内部计时器保存在context中,并以增量方式获取它函数来调用clearInterval()
。
答案 1 :(得分:0)
我这样解决了。请看看我的节点代码。还请提出是否可以采用其他更好的方法。
[
{
"id": "b6c9b219.90a478",
"type": "function",
"z": "a3d6aff.bd4935",
"name": "",
"func": "var i = 0;\nfunction increment(){\n i = i + 1;\n msg={value:i};\n node.send(msg);\n if(global.get(\"a\")===\"off\"){\n clearInterval(interval);\n }\n}\n\nif(msg.payload===true){\nvar interval = setInterval( increment, 1000);\n}",
"outputs": 1,
"noerr": 0,
"x": 450.5000305175781,
"y": 508,
"wires": [
[
"12f2090c.587347"
]
]
},
{
"id": "12f2090c.587347",
"type": "debug",
"z": "a3d6aff.bd4935",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"x": 699.5,
"y": 686.9999694824219,
"wires": []
},
{
"id": "26abbbf.05cf944",
"type": "inject",
"z": "a3d6aff.bd4935",
"name": "",
"topic": "",
"payload": "true",
"payloadType": "bool",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 305.8833312988281,
"y": 430.2833557128906,
"wires": [
[
"b6c9b219.90a478",
"4fdb56dd.8ef"
]
]
},
{
"id": "880341a0.dcab2",
"type": "inject",
"z": "a3d6aff.bd4935",
"name": "",
"topic": "",
"payload": "false",
"payloadType": "bool",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 207.88333129882812,
"y": 666.0999450683594,
"wires": [
[
"4fdb56dd.8ef",
"b6c9b219.90a478"
]
]
},
{
"id": "4fdb56dd.8ef",
"type": "function",
"z": "a3d6aff.bd4935",
"name": "",
"func": "if(msg.payload===false){\n global.set(\"a\",\"off\");\n}\nif (msg.payload===true){\n global.set(\"a\",\"on\");\n}\nmsg={payload:global.get(\"a\")}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 202.26666259765625,
"y": 554.2332458496094,
"wires": [
[]
]
}
]