# Two msg in delay with function node

**URL:** <https://discourse.nodered.org/t/two-msg-in-delay-with-function-node/30435>\
**Category:** General\
**Created:** [21 July 2020 02:28 UTC](https://discourse.nodered.org/t/two-msg-in-delay-with-function-node/30435 "2020-07-21T02:28:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [21 July 2020 02:28 UTC](https://discourse.nodered.org/t/two-msg-in-delay-with-function-node/30435/1 "2020-07-21T02:28:22Z")

</div>

Hi guys, I'm trying to send two messages from one funcion it's kind off something like this....

```auto
msg.payload = "hi";
msg.delay = 5000;
msg.payload = "goodbye";
msg.delay = 10000;
return msg;

```

basically I have a msg to send after the first one, I was looking for some information because I saw it before the msg.delay function. Some help please I din't found anything else

---

<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:** [21 July 2020 04:39 UTC](https://discourse.nodered.org/t/two-msg-in-delay-with-function-node/30435/2 "2020-07-21T04:39:11Z")

</div>

There are two ways you can do this.

1 - the `function` node does it all  
2 - you add a `delay` node after the `function` node.

This is a flow that shows both ways.

```auto
[{"id":"bbda58b0.80b1d8","type":"function","z":"2a10ff07.234f98","name":"","func":"msg.payload = \"First message\";\nnode.send(msg);\nmsg.payload = \"Last message\"\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":1860,"wires":[["46e569fc.bd4918"]]},{"id":"e97297e2.98a31","type":"inject","z":"2a10ff07.234f98","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":1810,"wires":[["db5ebf42.239088","bbda58b0.80b1d8"]]},{"id":"46e569fc.bd4918","type":"delay","z":"2a10ff07.234f98","name":"Delay","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"5","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":480,"y":1860,"wires":[["34c57a8d.7684d6"]]},{"id":"db5ebf42.239088","type":"function","z":"2a10ff07.234f98","name":"","func":"//\n// Note the position of the first and last messages.\n//\nsetTimeout(function(){\n msg.payload = \"last message\";\n node.send(msg);\n}, 5000); // This is how many milliseconds to wait.\n\nmsg.payload = \"first message\";\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":1810,"wires":[["7c1fbeb3.10b04"]]},{"id":"34c57a8d.7684d6","type":"debug","z":"2a10ff07.234f98","name":"Second way","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":670,"y":1860,"wires":[]},{"id":"7c1fbeb3.10b04","type":"debug","z":"2a10ff07.234f98","name":"First way","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":660,"y":1810,"wires":[]}]

```

The only problem is that there is an initial `5` second delay for the first message too.

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [21 July 2020 05:02 UTC](https://discourse.nodered.org/t/two-msg-in-delay-with-function-node/30435/3 "2020-07-21T05:02:13Z")

</div>

Aside from the delay issue (for which you should use something like the Delay node), your example won't work in a Function node because the first payload gets overwritten. I suggest you read up on using Functions ... [https://nodered.org/docs/user-guide/writing-functions](https://nodered.org/docs/user-guide/writing-functions).

An uncomplicated flow would be: Inject -\>Function -\>Delay -\>Function -\> Delay -\> Debug

Alternatively,have a read of [Can I have a delay / sleep inside Function node](https://discourse.nodered.org/t/can-i-have-a-delay-sleep-inside-function-node/3101)

---

<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:** [21 July 2020 05:04 UTC](https://discourse.nodered.org/t/two-msg-in-delay-with-function-node/30435/4 "2020-07-21T05:04:10Z")

</div>

Sorry, me or @lalo?

---

<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:** [4 August 2020 05:18 UTC](https://discourse.nodered.org/t/two-msg-in-delay-with-function-node/30435/5 "2020-08-04T05:18:51Z")

</div>

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