# Delay - wait in loop/function

**URL:** <https://discourse.nodered.org/t/delay-wait-in-loop-function/33508>\
**Category:** General\
**Created:** [29 September 2020 13:17 UTC](https://discourse.nodered.org/t/delay-wait-in-loop-function/33508 "2020-09-29T13:17:56Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![RogierQ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rogierq/32/3211_2.png) [@RogierQ](https://discourse.nodered.org/u/RogierQ)\
**Post date:** [29 September 2020 13:17 UTC](https://discourse.nodered.org/t/delay-wait-in-loop-function/33508/1 "2020-09-29T13:17:56Z")

</div>

Hi!

I need to make a slot machine effect  
So a running light over 4 outputs the goes slower and slower  
I thought to do this in a loop but i cannot get the delay working.

for example i did this:

```auto
msg.payload = "test1"
node.send(msg);

delay 1000;

msg.payload = "test2"
node.send(msg);

delay 1000;

msg.payload = "test3"
node.send(msg);

```

But that gives errors.  
Is that possible to do?  
I found some posts with the timeout function but have no idea how to include this in a loop

Is it possible to use delay or wait or something?

---

<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:** [29 September 2020 13:53 UTC](https://discourse.nodered.org/t/delay-wait-in-loop-function/33508/2 "2020-09-29T13:53:22Z")

</div>

Hello,  
There is no native delay or wait function in javascript so this approach will not work like it would in c or python. You most likely will have to approach it differently. Some ideas I have are you could try move the delays outside of the function node and use something like the trigger or delay node to achieve your timeout and feedback into the function from its output, this would be utilizing a very nodered way of doing things. Another option would be to read more about the setTimeout method in JavaScript to achieve it.  
For the first idea you could utilize a node context variable to keep track of how many loops/round trips you have run of the function and the delay node. Here is a simple example of this approach:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/0/003ac59a33964af4fb348d06ac5ae5209364d0e3.jpeg)

```auto
[{"id":"5331bb12.a5ac24","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"fdff5d7d.9e8ce8","type":"inject","z":"5331bb12.a5ac24","name":"Number of loops","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"4","payloadType":"num","x":270,"y":140,"wires":[["45f326c3.1fde28"]]},{"id":"45f326c3.1fde28","type":"function","z":"5331bb12.a5ac24","name":"","func":"let loops = context.get(\"loops\") || 1;\nif (msg.topic !== \"running\"){\n context.set(\"loops\", msg.payload - 1);\n msg.topic = \"running\";\n return [msg, msg]\n} else if (loops !== 1) {\n msg.payload = loops;\n context.set(\"loops\", loops - 1);\n return [msg, msg];\n} else {\n msg.payload = loops;\n return [msg, null];\n}\nreturn msg;","outputs":2,"noerr":0,"initialize":"","finalize":"","x":480,"y":140,"wires":[["cbc26d74.e76d08"],["63da4291.62cc44"]]},{"id":"63da4291.62cc44","type":"delay","z":"5331bb12.a5ac24","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":480,"y":200,"wires":[["45f326c3.1fde28"]]},{"id":"cbc26d74.e76d08","type":"debug","z":"5331bb12.a5ac24","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":140,"wires":[]}]

```

Maybe this can give you some ideas.

Johannes

---

<div class="post-metadata">

**Author:** ![softy2k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/softy2k/32/23707_2.png) [@softy2k](https://discourse.nodered.org/u/softy2k)\
**Post date:** [29 September 2020 13:56 UTC](https://discourse.nodered.org/t/delay-wait-in-loop-function/33508/3 "2020-09-29T13:56:13Z")

</div>

Another idea

```auto
[{"id":"949f8f0b.827c38","type":"tab","label":"Delay","disabled":false,"info":""},{"id":"e68e112c.5b28f8","type":"inject","z":"949f8f0b.827c38","name":"Go","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":330,"y":300,"wires":[["dc6090a3.f445","d3b39a35.693538","474719d8.2d241"]]},{"id":"dc6090a3.f445","type":"function","z":"949f8f0b.827c38","name":"Test 1","func":"msg.payload = 'Test 1';\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":530,"y":240,"wires":[["223d668e.2dddda"]]},{"id":"223d668e.2dddda","type":"delay","z":"949f8f0b.827c38","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":670,"y":240,"wires":[["b2487ed2.86f138"]]},{"id":"b2487ed2.86f138","type":"function","z":"949f8f0b.827c38","name":"Other Functions","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":930,"y":300,"wires":[["b8394c14.1b7858"]]},{"id":"d3b39a35.693538","type":"function","z":"949f8f0b.827c38","name":"Test 2","func":"msg.payload = 'Test 2';\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":530,"y":300,"wires":[["379007f8.ce411"]]},{"id":"379007f8.ce411","type":"delay","z":"949f8f0b.827c38","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":670,"y":300,"wires":[["b2487ed2.86f138"]]},{"id":"474719d8.2d241","type":"function","z":"949f8f0b.827c38","name":"Test 3","func":"msg.payload = 'Test 3';\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":530,"y":360,"wires":[["1551632a.4fb295"]]},{"id":"1551632a.4fb295","type":"delay","z":"949f8f0b.827c38","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":670,"y":360,"wires":[["b2487ed2.86f138"]]},{"id":"b8394c14.1b7858","type":"debug","z":"949f8f0b.827c38","name":"Final","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1130,"y":300,"wires":[]}]

```

---

<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:** [29 September 2020 14:01 UTC](https://discourse.nodered.org/t/delay-wait-in-loop-function/33508/4 "2020-09-29T14:01:15Z")

</div>

I think this approach although very simple might get messy very quickly if you try to apply it to more complex examples which require a longer loop and maybe although a variable changing delay which I think is what @RogierQ will need for his slot machine but it definitely is a quick workaround 👍😊

---

<div class="post-metadata">

**Author:** ![softy2k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/softy2k/32/23707_2.png) [@softy2k](https://discourse.nodered.org/u/softy2k)\
**Post date:** [29 September 2020 14:05 UTC](https://discourse.nodered.org/t/delay-wait-in-loop-function/33508/5 "2020-09-29T14:05:47Z")

</div>

> [@JGKK](#):
>
> quick workaround

Very quick solution, indeed.  
But for 3 or 4 light bulbs it's simple enough. For multiple bulbs, not applicable.

---

<div class="post-metadata">

**Author:** ![RogierQ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rogierq/32/3211_2.png) [@RogierQ](https://discourse.nodered.org/u/RogierQ)\
**Post date:** [29 September 2020 14:07 UTC](https://discourse.nodered.org/t/delay-wait-in-loop-function/33508/6 "2020-09-29T14:07:54Z")

</div>

Thanks for your ideas!  
Only thing is that in the examples the delay is static, but the delay needs to get longer after every step.  
so it starts fast and stops slowerrrrr  
I cannot set the timing parameter with a external value from the script

---

<div class="post-metadata">

**Author:** ![softy2k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/softy2k/32/23707_2.png) [@softy2k](https://discourse.nodered.org/u/softy2k)\
**Post date:** [29 September 2020 14:10 UTC](https://discourse.nodered.org/t/delay-wait-in-loop-function/33508/7 "2020-09-29T14:10:26Z")

</div>

Take a look :

> [@Delay node with variable delay](https://discourse.nodered.org/t/delay-node-with-variable-delay/18486/7):
>
> Ok, I made a big mistake with my reply. I made it. Not you. The msg.delay needs to be sent with the message. So: If you send msg.delay 3000 then send msg.payload Hello through the delay node as is, it will default to 1000 ms. What you need to do is send a message with both the payload and the delay parts. This is tricky because you need to store the delay value for subsequent messages. I don't know how much you know about Node-Red. (No problem. We all start at the bottom and work up.) …

> [@How to both reset & set delay in "delay node"](https://discourse.nodered.org/t/how-to-both-reset-set-delay-in-delay-node/14999/4):
>
> Thank you, it works , but I needed to increase delay, 8ms was not reliable enough,

> [@DELAY node - variable delay not seeming to work](https://discourse.nodered.org/t/delay-node-variable-delay-not-seeming-to-work/14603/4):
>
> Use a flow context variable to store the your desired message delay value. Then use a Change node immediately before the Delay node to set msg.delay to the value of that flow context variable.

---

<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:** [29 September 2020 14:19 UTC](https://discourse.nodered.org/t/delay-wait-in-loop-function/33508/8 "2020-09-29T14:19:13Z")

</div>

You can set the delay by adding a msg.delay property to the msg you send from the function node and setting the delay node accordingly.  
Here is the same example with the delay being set from the function node and getting longer:

```auto
[{"id":"5331bb12.a5ac24","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"fdff5d7d.9e8ce8","type":"inject","z":"5331bb12.a5ac24","name":"Number of loops","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"4","payloadType":"num","x":270,"y":140,"wires":[["45f326c3.1fde28"]]},{"id":"45f326c3.1fde28","type":"function","z":"5331bb12.a5ac24","name":"","func":"let loops = context.get(\"loops\") || 1;\nlet delay = context.get(\"delay\");\nif (msg.topic !== \"running\"){\n context.set(\"loops\", msg.payload - 1);\n msg.topic = \"running\";\n msg.delay = 1000;\n context.set(\"delay\", msg.delay);\n return [msg, msg]\n} else if (loops !== 1) {\n msg.payload = loops;\n msg.delay = delay + 500;\n context.set(\"loops\", loops - 1);\n context.set(\"delay\", msg.delay);\n return [msg, msg];\n} else {\n msg.payload = loops;\n return [msg, null];\n}\nreturn msg;","outputs":2,"noerr":0,"initialize":"","finalize":"","x":480,"y":140,"wires":[["cbc26d74.e76d08"],["63da4291.62cc44"]]},{"id":"63da4291.62cc44","type":"delay","z":"5331bb12.a5ac24","name":"","pauseType":"delayv","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":480,"y":200,"wires":[["45f326c3.1fde28"]]},{"id":"cbc26d74.e76d08","type":"debug","z":"5331bb12.a5ac24","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":140,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [29 September 2020 20:09 UTC](https://discourse.nodered.org/t/delay-wait-in-loop-function/33508/9 "2020-09-29T20:09:52Z")

</div>

You should be able to simulate a delay (without blocking Node-RED from processing other things meanwhile) by using `async/await`:

```auto
async function delay(timeMs) {
  await new Promise(resolve => setTimeout(resolve, timeMs));
}

msg.payload = "test1"
node.send(msg);

await delay(1000);

msg.payload = "test2"
node.send(msg);

```

(I didn't test this)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [29 September 2020 20:24 UTC](https://discourse.nodered.org/t/delay-wait-in-loop-function/33508/10 "2020-09-29T20:24:41Z")

</div>

> [@ristomatti](#):
>
> `await new Promise(resolve => setTimeout(resolve, timeMs));`

i was just recently studying about promises and the syntax is to short to fully understand the resolve

but shouldnt that be `return new Promise(resolve => setTimeout(resolve, timeMs));`

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [29 September 2020 20:35 UTC](https://discourse.nodered.org/t/delay-wait-in-loop-function/33508/11 "2020-09-29T20:35:55Z")

</div>

It might not make a difference in this case. As the `async delay()` returns a `Promise`. The `Promise` is resolved when the part of the function that awaits the manually created promise to resolve (after the timeout).

But if both work, your suggestion would avoid rhe second promise layer. TBH I just did a quick search to find a confirmation to what I had seen before and modified the first result to fit this use case as I'm on my tablet.

The `async/await` syntax is just "syntactic sugar" to make promise handling look more like synchronous code execution and it actually comes with some overhead (likely not something that would matter in most cases).

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 September 2020 06:28 UTC](https://discourse.nodered.org/t/delay-wait-in-loop-function/33508/12 "2020-09-30T06:28:35Z")

</div>

Something I wrote got mentioned back in post 6.

The _problem_ if you use that is that the delay would have to start off small and become bigger.

But it seems like you hare making good progress on it.

Good luck.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [29 November 2020 06:28 UTC](https://discourse.nodered.org/t/delay-wait-in-loop-function/33508/13 "2020-11-29T06:28:36Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
