# How to delay in a while loop?

**URL:** https://discourse.nodered.org/t/how-to-delay-in-a-while-loop/17108
**Category:** General
**Created:** [24 October 2019 21:31 UTC](https://discourse.nodered.org/t/how-to-delay-in-a-while-loop/17108 "2019-10-24T21:31:47Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)
#### Post date: [24 October 2019 21:31 UTC](https://discourse.nodered.org/t/how-to-delay-in-a-while-loop/17108/1 "2019-10-24T21:31:47Z")

</div>

Hiho, here I come with another problem:  
This will kill the connection because of running in a loop without delay.  
What to do now ?

```auto
while(utsRelayOff > utsCurrent)
{
    utsCurrent = parseInt(+new Date());
    utsRelayOff = flow.get("utsRelayOff_t");
    //sleep(10);
}

```

btw: the delay node is useless until I can not break it / or change the delay time,

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [24 October 2019 21:45 UTC](https://discourse.nodered.org/t/how-to-delay-in-a-while-loop/17108/2 "2019-10-24T21:45:44Z")

</div>

> the delay node is useless until I can not break it / or change the delay time,

The delay node is quite fantastic.

But what about explaining your topic, what are you trying to do ?  
What is the plan for the while loop ?

We cannot guess what you are trying to create.

---

<div class="post-metadata">

### Author: ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)
#### Post date: [24 October 2019 21:56 UTC](https://discourse.nodered.org/t/how-to-delay-in-a-while-loop/17108/3 "2019-10-24T21:56:23Z")

</div>

Well its not hard to understand:  
utsCurrent is the current time

utsRelayOff is a flow value that will be changed while in the loop by another function.  
This will happen if a motion is detected by a PiR Sensor ... so if a motion is detected the time for sending a message to switch off the relay / light should be delayed.

The background is to send a message that will switch off a relay -\> light. The flow is big and its not usefull to post I think.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [24 October 2019 21:59 UTC](https://discourse.nodered.org/t/how-to-delay-in-a-while-loop/17108/4 "2019-10-24T21:59:51Z")

</div>

That sounds more complicated than it has to be.

Look into the trigger node. if PIR, send nothing, wait for x time (extend the delay), time passed, send off-message.

Also note that a while loop within a node-red function is not recommended as you potentially block the flow (ie single thread). I wonder why you need loops in your flow. Are you coming from a programming perspective ?

---

<div class="post-metadata">

### Author: ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)
#### Post date: [25 October 2019 08:22 UTC](https://discourse.nodered.org/t/how-to-delay-in-a-while-loop/17108/5 "2019-10-25T08:22:16Z")

</div>

Yes I am a coder and sometime its hard to use node where a solution by code is so easy.  
Thanx for reply. Using a trigger node didn´t do it for me. I used a function node with 2 outputs plus a a delay node to emulate the while loop and used another function node to build the code outside of the while loop around. but thats much overhead just because of missing the support of delay in node red.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [25 October 2019 08:28 UTC](https://discourse.nodered.org/t/how-to-delay-in-a-while-loop/17108/6 "2019-10-25T08:28:22Z")

</div>

Why didn't the Trigger node do it for you? It is designed for exactly the use you've described;

- receive a message (a trigger from your PIR), wait X seconds until sending the 'off' message
- if another message is received from the PIR, extend the delay

> [@WhiteLion](#):
>
> missing the suppurt of delay in node red.

Well, that's JavaScript for you. There are plenty of ways of achieving a delay - just not in the way you want to do it.

---

<div class="post-metadata">

### Author: ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)
#### Post date: [25 October 2019 08:40 UTC](https://discourse.nodered.org/t/how-to-delay-in-a-while-loop/17108/7 "2019-10-25T08:40:27Z")

</div>

Javascript is new for me too... which ways are there to sleep in js in my case ?  
I dont like node with much options since debugging is not possible in a classic way I did avoid the trigger node and took what was clear to me to get a fast result. - My wife is on my neck because of the not working lights at my home 🙂  
I hope to find the time to try trigger node later on.

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [25 October 2019 08:52 UTC](https://discourse.nodered.org/t/how-to-delay-in-a-while-loop/17108/8 "2019-10-25T08:52:13Z")

</div>

Simply speaking, in JS everything is event-driven. Events are processed by the event loop, only one event ("piece of code") can run at any time there. Running CPU intensive tasks or things like your while-loop will block the event loop and your whole runtime will lock, as you have observed.

This is where callbacks come in. E.g., you can use `setTimeout()` to execute a piece of code after a specific time by specifying a callback function and a timeout. This way, your code will not block the event loop. That's what the `delay` and `trigger` nodes are using under the hood.

Here is some light reading on the topic: 😀

> **[The Node.js Event Loop, Timers, and process.nextTick() | Node.js](https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/)**
>
> Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [25 October 2019 08:52 UTC](https://discourse.nodered.org/t/how-to-delay-in-a-while-loop/17108/9 "2019-10-25T08:52:35Z")

</div>

Perhaps you have a misconception on what node-red does and how to use it.

Yes you can use programming (JS) in functions for more customized control, this should not be needed for your example (unless we are missing some information).

Node-red in a nutshell to me: a message has a lifecycle, comes in at point A and goes to point B or any other direction (to the right), no loops.  
You connect nodes together to make this work. Need input from other nodes accross flows ? Use link nodes. Personally, I consider variables a last resort, function nodes as well.

Your PIR example is simple if you use the principle above.

My house is full with PIR's and triggers together with all kinds of conditions in between them. No function nodes needed.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [25 October 2019 08:59 UTC](https://discourse.nodered.org/t/how-to-delay-in-a-while-loop/17108/10 "2019-10-25T08:59:35Z")

</div>

> [@WhiteLion](#):
>
> Javascript is new for me too... which ways are there to sleep in js in my case ?  
> I dont like node with much options since debugging is not possible in a classic way I did avoid the trigger node and took what was clear to me to get a fast result.

Fair enough - except in this instance the 'fast result' isn't happening because you are trying to do something in a language you are not familiar with. We have pointed out the Trigger node a couple times now as being the ideal fit for what you want. You could have the result working in a couple minutes if you tried it and got your lights working. That doesn't stop you pursuing a code-only approach if you really wanted to in parallel... but I suspect once you see how easy the Trigger node makes it, you'd realise it wasn't worth reinventing its functionality in code you'd have to maintain.
