how to stop setInterval in JS in node-red..?

跟風遠走 提交于 2021-01-29 06:52:00

问题


I'm trying to code a function node in node-red which will give output as increasing numbers by 1 for every second when get msg.paylaod true from inject node and stop when get msg.payload "false" from another inject node. it starts giving output but don't stop when payload "false" is injected.

Node code:

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

Code of function node:

var i = 1;
function increment(){
    i = i + 1;
    msg={value:i};
    node.send(msg);
     if(msg.payload===false){
     clearInterval(interval);
   }

}
if(msg.payload===true){
var interval = setInterval( increment, 1000);

}

回答1:


The short answer is you don't (easily).

The better way to do this is by having the function send all the messages in the sequence at once using the format for sending multiple messages. Then use the delay node to rate limit the stream so the messages are released once a second.

If you REALLY need (you really don't) to do it the way you have it then you need to save the internal timer in the context and retrieve it in the increment function to call clearInterval() on it.




回答2:


I solved it like this. please take look at my node code. also please suggest if it can be done some other better way.

[
  {
    "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": [
      []
    ]
  }
]


来源:https://stackoverflow.com/questions/55566703/how-to-stop-setinterval-in-js-in-node-red

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!