How should I start and stop a loop with a Switch Node?

Hello, I am new to Node-Red but I have a flow that controls and receives data from some instrument logic. I have a "Single Event Trigger" Button that acquires data and plots it using a Chart. I want to add a "Continuous Trigger" Switch that runs the process in a loop, updating the Chart as fast as possible.

My first attempt was to make a Function Node with a while(msg.payload) { } loop. The msg came from a Switch node. Unfortunately, once the loop started, the whole flow became unresponsive and I could not throw the Switch.

What is the correct way to do something like this?

Generally, you would not want a loop for this kind of thing in a flow-based tool.

Instead, you use a flag of some sort with a gate. The gate can be the switch node you mention.

The flag itself could either be a property on the msgs coming from you instrument logic or it could be something like a flow context variable and set manually or via some other flow.

If the flag indicates that you want continuous data, then keep sending messages via the switch node to the chart, otherwise send nothing.

I guess I don't understand your advice. Is there a good example of actions that repeat when enabled by the user and then stop when disabled by the user?

Try this simple flow that illustrates the approach.

[{"id":"c24cf373d2aa0a7b","type":"switch","z":"b2f18a716bd20f99","name":"","property":"GATE","propertyType":"flow","rules":[{"t":"true"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":470,"y":320,"wires":[["f871a3d9b1467562"],[]]},{"id":"4ea216504635bdd0","type":"inject","z":"b2f18a716bd20f99","name":"","props":[{"p":"payload"}],"repeat":"3","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":320,"wires":[["c24cf373d2aa0a7b"]]},{"id":"f871a3d9b1467562","type":"debug","z":"b2f18a716bd20f99","name":"debug 12","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":660,"y":300,"wires":[]},{"id":"5ebcc9f8f839a028","type":"inject","z":"b2f18a716bd20f99","name":"Start","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":210,"y":420,"wires":[["58ccad5d0f9524ee"]]},{"id":"a2c23ef053e0ab2e","type":"inject","z":"b2f18a716bd20f99","name":"Stop","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":210,"y":480,"wires":[["58ccad5d0f9524ee"]]},{"id":"58ccad5d0f9524ee","type":"change","z":"b2f18a716bd20f99","name":"","rules":[{"t":"set","p":"GATE","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":440,"wires":[[]]}]