# Timer counts to a label

**URL:** <https://discourse.nodered.org/t/timer-counts-to-a-label/36921>\
**Category:** General\
**Created:** [3 December 2020 01:58 UTC](https://discourse.nodered.org/t/timer-counts-to-a-label/36921 "2020-12-03T01:58:36Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [3 December 2020 06:42 UTC](https://discourse.nodered.org/t/timer-counts-to-a-label/36921/3 "2020-12-03T06:42:04Z")

</div>

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()`](https://javascript.info/settimeout-setinterval#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](https://nodered.org/docs/user-guide/context) 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:

> [@Ideas for elapsed time counter please](https://discourse.nodered.org/t/ideas-for-elapsed-time-counter-please/35352/2):
>
> Hello, Maybe this could be useful for you. It’s not a custom node but nearly wink This is a subflow I build: [{"id":"60288551.9399d4","type":"subflow","name":"elapsedTime","info":"","category":"","in":[{"x":60,"y":60,"wires":[{"id":"2cdff5f2.09a772"}]}],"out":[{"x":620,"y":60,"wires":[{"id":"287cb861.3f1bc","port":0}]}],"env":[{"name":"targetTime","type":"num","value":"0","ui":{"icon":"font-awesome/fa-history","label":{"en-US":"Target Time"},"type":"input","opts":{"types":["num"]}…

Hope this gives you some ideas, Johannes

---

_[View the full topic](https://discourse.nodered.org/t/timer-counts-to-a-label/36921)._
