# Delay inside function node between messages?

**URL:** https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736
**Category:** General
**Created:** [6 October 2018 13:22 UTC](https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736 "2018-10-06T13:22:27Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![dakure](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@dakure](https://discourse.nodered.org/u/dakure)
#### Post date: [6 October 2018 13:22 UTC](https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736/1 "2018-10-06T13:22:27Z")

</div>

Hi! I am a newbie of NodeRed and loving it so far. Thank you all for this amazing tool!  
In this example from documentation is it posible to have a delays between messages inside the function node?

```
var msg1 = { payload:"first out of output 1" };
var msg2 = { payload:"second out of output 1" };
var msg3 = { payload:"third out of output 1" };
var msg4 = { payload:"only message from output 2" };
return [[ msg1, msg2, msg3], msg4 ];

```

thx! 🙂

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [6 October 2018 14:00 UTC](https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736/2 "2018-10-06T14:00:14Z")

</div>

Yes, but it involves some slightly more advanced javascript, using setTimeout. To avoid this you could send them all to separate outputs then use delay nodes on the outputs and merge them back together again.

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [6 October 2018 14:07 UTC](https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736/3 "2018-10-06T14:07:27Z")

</div>

Another non-blocking option would be to use a 'triggered flow'.

An example of a triggered flow can be found [https://gist.github.com/Paul-Reed/c29cfa755fed666805956eb6da42a0d0](https://gist.github.com/Paul-Reed/c29cfa755fed666805956eb6da42a0d0) which I use to ensure that the flow carries out instructions in a set order - and when the previous instruction has completed.

Paul

EDIT// A better example of a triggered flow -[https://gist.github.com/dceejay/cea8afa28b7a93ebdc0f](https://gist.github.com/dceejay/cea8afa28b7a93ebdc0f)

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [6 October 2018 16:05 UTC](https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736/4 "2018-10-06T16:05:33Z")

</div>

Yet another option, haven't tried it myself, but take a look at [https://flows.nodered.org/node/node-red-contrib-simple-message-queue](https://flows.nodered.org/node/node-red-contrib-simple-message-queue)

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [6 October 2018 18:19 UTC](https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736/5 "2018-10-06T18:19:46Z")

</div>

This should do what you want, using just the `delay` node.

```auto
[{"id":"55680c13.4953a4","type":"debug","z":"7920414b.cfa4f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":650,"y":120,"wires":[]},{"id":"85c8db46.a1272","type":"function","z":"7920414b.cfa4f8","name":"","func":"// msg.delay is in milliseconds\nvar msg1 = { payload:\"first out of output 1\", delay:10000 };\n// if msg.delay is undefined, the delay node uses default\nvar msg2 = { payload:\"second out of output 1\" };\nvar msg3 = { payload:\"third out of output 1\", delay:0};\nvar msg4 = { payload:\"only message from output 2\"};\nreturn [[ msg1, msg2, msg3], msg4 ];","outputs":2,"noerr":0,"x":370,"y":160,"wires":[["c600d120.54da78"],["88475b70.b765b"]]},{"id":"3b9f76b1.35359a","type":"inject","z":"7920414b.cfa4f8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":160,"wires":[["85c8db46.a1272"]]},{"id":"c600d120.54da78","type":"delay","z":"7920414b.cfa4f8","name":"","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":520,"y":120,"wires":[["55680c13.4953a4"]]},{"id":"88475b70.b765b","type":"debug","z":"7920414b.cfa4f8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":510,"y":200,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [6 October 2018 18:53 UTC](https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736/6 "2018-10-06T18:53:08Z")

</div>

> [@drmibell](#):
>
> This should do what you want, using just the `delay` node.

The resulting order is incorrect;

![sweet](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/01ff020bd4957bb66fab4eb207a4af94aef6ca03.png)

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [6 October 2018 19:21 UTC](https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736/7 "2018-10-06T19:21:45Z")

</div>

@Paul-Reed No, the delays were set deliberately in the `function` node to show what could be done: msg1 delayed by 10 s, msg2 with default (5 s), and msg3 with no delay. Clearly, if you want to preserve the order you have to set progressively increasing delays. Sorry for the confusion.

---

<div class="post-metadata">

### Author: ![dakure](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@dakure](https://discourse.nodered.org/u/dakure)
#### Post date: [8 October 2018 16:38 UTC](https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736/8 "2018-10-08T16:38:36Z")

</div>

Hi, This is what I was trying but only find some examples in node.status... inside function before return msg.  
So is it posible to have timeout inside the return? You have a simple example or some link to learn how? I tried but I can t figure out...  
Thank you again.

---

<div class="post-metadata">

### Author: ![dakure](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@dakure](https://discourse.nodered.org/u/dakure)
#### Post date: [8 October 2018 16:42 UTC](https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736/9 "2018-10-08T16:42:16Z")

</div>

Thank you Paul. I will try. I just need to figure out so many things first. In this case would be much better for me just to set timeouts on the return msg if is posible.  
Any link o reference will be very helpful... Thank you again.

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [8 October 2018 18:42 UTC](https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736/10 "2018-10-08T18:42:13Z")

</div>

If you choose to use a 'triggered flow', the easiest option is to use [node-red-contrib-simple-message-queue](https://flows.nodered.org/node/node-red-contrib-simple-message-queue) and there is information in the node readme about the various options available.  
This node is basically @dceejay's 'triggered flow' example wrapped up into a node, and works on the same principle.

If you want a simple flow to demonstrate it working, install the `node-red-contrib-simple-message-queue` node (it's in the node-RED library), and then import the flow below.

Click the inject node and 5 messages will be injected into the flow, the first message will travel straight through the 'queue' node, and the remaining four will be queued, and released in sequence, as the preceding message acknowledges that it's been completed.  
The delay node slows down the flow, so you can see what's happening, and simulates time taken to complete each action.

```auto
[
    {
        "id": "ca4b5b70.5a0678",
        "type": "simple-queue",
        "z": "c53060.842a0fa",
        "name": "queue",
        "firstMessageBypass": true,
        "bypassInterval": "30000",
        "x": 620,
        "y": 790,
        "wires": [
            [
                "ec60a83b.a96518"
            ]
        ]
    },
    {
        "id": "ae67f50a.990b88",
        "type": "change",
        "z": "c53060.842a0fa",
        "name": "release next msg",
        "rules": [
            {
                "t": "set",
                "p": "trigger",
                "pt": "msg",
                "to": "1",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 620,
        "y": 720,
        "wires": [
            [
                "ca4b5b70.5a0678"
            ]
        ]
    },
    {
        "id": "ec60a83b.a96518",
        "type": "delay",
        "z": "c53060.842a0fa",
        "name": "simulate delay in running function",
        "pauseType": "delay",
        "timeout": "3",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "x": 850,
        "y": 790,
        "wires": [
            [
                "ae67f50a.990b88",
                "3ecbc006.f8586"
            ]
        ]
    },
    {
        "id": "8e3df100.38b86",
        "type": "inject",
        "z": "c53060.842a0fa",
        "name": "",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 240,
        "y": 790,
        "wires": [
            [
                "b49e71eb.d2c9b"
            ]
        ]
    },
    {
        "id": "3ecbc006.f8586",
        "type": "debug",
        "z": "c53060.842a0fa",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "x": 940,
        "y": 840,
        "wires": []
    },
    {
        "id": "b49e71eb.d2c9b",
        "type": "function",
        "z": "c53060.842a0fa",
        "name": "Create some msg's",
        "func": "var msg1 = { payload:\"first out of output\" };\nvar msg2 = { payload:\"second out of output\" };\nvar msg3 = { payload:\"third out of output\" };\nvar msg4 = { payload:\"fourth out of output\" };\nvar msg5 = { payload:\"fifth out of output\" };\nreturn [[msg1, msg2, msg3, msg4, msg5]];",
        "outputs": 2,
        "noerr": 0,
        "x": 430,
        "y": 790,
        "wires": [
            [
                "ca4b5b70.5a0678"
            ],
            []
        ]
    }
]

```

---

<div class="post-metadata">

### Author: ![dakure](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@dakure](https://discourse.nodered.org/u/dakure)
#### Post date: [8 October 2018 19:36 UTC](https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736/11 "2018-10-08T19:36:46Z")

</div>

Hi!  
I tried and I see that is working but I really don t understand why... This release next message cross flow ... it s too much. With this setup is it necessary the delay?  
I would love also to have control of the time between the messages... not to a fixed interval... let s say 5 seconds and after another message after 15 seconds... just a simple java timeout...  
Thanks for your help anyway... I will use this triggered flow in another setup for the garden.  
Thank you again

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [8 October 2018 19:55 UTC](https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736/12 "2018-10-08T19:55:45Z")

</div>

> [@dakure](#):
>
> I would love also to have control of the time between the messages... not to a fixed interval... let s say 5 seconds and after another message after 15 seconds

Just reconfigure the node to set the bypass interval, like this;

```auto
[
    {
        "id": "ca4b5b70.5a0678",
        "type": "simple-queue",
        "z": "c53060.842a0fa",
        "name": "queue",
        "firstMessageBypass": true,
        "bypassInterval": "15000",
        "x": 620,
        "y": 790,
        "wires": [
            [
                "3ecbc006.f8586"
            ]
        ]
    },
    {
        "id": "8e3df100.38b86",
        "type": "inject",
        "z": "c53060.842a0fa",
        "name": "",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 240,
        "y": 790,
        "wires": [
            [
                "b49e71eb.d2c9b"
            ]
        ]
    },
    {
        "id": "3ecbc006.f8586",
        "type": "debug",
        "z": "c53060.842a0fa",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "x": 800,
        "y": 790,
        "wires": []
    },
    {
        "id": "b49e71eb.d2c9b",
        "type": "function",
        "z": "c53060.842a0fa",
        "name": "Create some msg's",
        "func": "var msg1 = { payload:\"first out of output\" };\nvar msg2 = { payload:\"second out of output\" };\nvar msg3 = { payload:\"third out of output\" };\nvar msg4 = { payload:\"fourth out of output\" };\nvar msg5 = { payload:\"fifth out of output\" };\nreturn [[msg1, msg2, msg3, msg4, msg5]];",
        "outputs": 2,
        "noerr": 0,
        "x": 430,
        "y": 790,
        "wires": [
            [
                "ca4b5b70.5a0678"
            ],
            []
        ]
    }
]

```

---

<div class="post-metadata">

### Author: ![dakure](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@dakure](https://discourse.nodered.org/u/dakure)
#### Post date: [8 October 2018 20:51 UTC](https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736/13 "2018-10-08T20:51:08Z")

</div>

I understand but I need every message to have different times betwwen them... not always 15.  
Thanks anyway...

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [8 October 2018 21:07 UTC](https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736/14 "2018-10-08T21:07:29Z")

</div>

If you want to do it yourself in a function node then as I said you will have to use setTimeout() in the code. Search for setTimeout in this forum and you will find some examples. However, if you are a beginner at js, you may well have some problems with the concepts. Sometimes it is better to learn to walk before trying to run.

---

<div class="post-metadata">

### Author: ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)
#### Post date: [9 October 2018 01:35 UTC](https://discourse.nodered.org/t/delay-inside-function-node-between-messages/3736/15 "2018-10-09T01:35:03Z")

</div>

You can also do some split/join trickery and preserve your msg.parts.index property in each flow, dump the joined flows into elements in an array, and use the change node to use the array's elements to build your final desired result.

edit: you also need to preserve most of the other msg.parts properties, especially the msg.parts.id, though if there are any parts you intend to omit, you can do so and then change the msg.parts.count property on all of the various flows; doing so will make it so the join node will sit there and wait until it has the right number of parts with a matching msg.parts.id before sending the combined object/array onward. (I haven't tried this with significant delays between the parts, but this makes me want to test if it's non-blocking...)
