Timer counts to a label

It cant work like this as your loop will run forever and block the whole node event loop effectively killing nodered.
You have to understand two things here:

  • node.js which nodered runs on is single threaded so one thing happens at a time and if something like an infinite loop is happening it will never get to do anything else but the loop
  • everytime you send a msg to a function node a new instance of the function is created so your first node can never get the stop message as it will never no about it as it will arrive to a different instance

So keeping those two factors in mind you will have to use something like setInterval() instead of a while loop (which is mostly a bad idea in node.js, you should try to avoid blocking code patterns in general as they go against its nature) to write a function that lets say checks for the stop every 100 milliseconds and doesn’t block the eventloop in the meantime. Secondly you will have to use context storage which you can read in the documentation about as a variable saved to context can be accessed from every instance of the same function node. So when a stop message arrives set a variable accordingly in the nodes context and check that context var on every interval.
This should give you some starting pointers if you want to do everything within a function node.
You could also look at the concept of an external tick send by an inject at an interval triggering the checking for a stop message and otherwise sending the ellapsed time.
Have a look here at something using that concept:

Hope this gives you some ideas, Johannes

1 Like