# Multiple Messages with setTimeout

**URL:** <https://discourse.nodered.org/t/multiple-messages-with-settimeout/65007>\
**Category:** General\
**Created:** [12 July 2022 15:01 UTC](https://discourse.nodered.org/t/multiple-messages-with-settimeout/65007 "2022-07-12T15:01:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ninja\_007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ninja_007/32/64510_2.png) [@ninja\_007](https://discourse.nodered.org/u/ninja_007)\
**Post date:** [12 July 2022 15:01 UTC](https://discourse.nodered.org/t/multiple-messages-with-settimeout/65007/1 "2022-07-12T15:01:28Z")

</div>

How can i push settimeout function value as output in function node ?  
I want to send O1 as output-1 and O2/rest messages as output-2

Things I've tried

```auto
var O1=({payload: "hello1"});
var O2=({payload: "hello2"});
setTimeout(function(){
({payload: "hello3"});
}, 1000);
setTimeout(function(){
({payload: "hello4"});
}, 2000);

return [O1, O2];

```

if i add node.send then it sends output but via O1

```auto
var O1=({payload: "hello1"});
var O2=({payload: "hello2"});
var O3=setTimeout(function(){
({payload: "hello3"});
}, 1000);
var O4=setTimeout(function(){
({payload: "hello4"});
}, 2000);

return [O1, [ O2, O3, O4] ];

```

This throws error "[object Object]" as msg : Timeout

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 July 2022 15:11 UTC](https://discourse.nodered.org/t/multiple-messages-with-settimeout/65007/2 "2022-07-12T15:11:25Z")

</div>

It there a particular reason you are not doing this low-code (i.e. using delay node)?

Anyhow, a `msg` MUST be an object (`O3` and `O4` are the result of calling `setTimeout` which are simply positive integers)

If you REALLY must (or are simply trying to learn) then to send `O1` out of pin 1 and `O2`, `O3` & `O4` out of pin 2 then...

```javascript
const O1 = { payload: "hello1" };
const O2 = { payload: "hello2" };
setTimeout(function () {
    node.send([null, { payload: "hello3" }]);
}, 1000);
setTimeout(function () {
    node.send([null, { payload: "hello4" }]);
}, 2000);

return [O1, O2];

```

**EDIT** : Updated for typos - thanks @UnborN

---

<div class="post-metadata">

**Author:** ![ninja\_007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ninja_007/32/64510_2.png) [@ninja\_007](https://discourse.nodered.org/u/ninja_007)\
**Post date:** [12 July 2022 15:21 UTC](https://discourse.nodered.org/t/multiple-messages-with-settimeout/65007/3 "2022-07-12T15:21:29Z")

</div>

Yes , for learning purpose

Thanks for code but there is no output of hello3 and hello4 after timeout at pin 2

---

<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:** [12 July 2022 15:25 UTC](https://discourse.nodered.org/t/multiple-messages-with-settimeout/65007/4 "2022-07-12T15:25:23Z")

</div>

There was typo .. should be `node.send([{ payload: "hello3" }, null]);`  
also for `node.send([null, { payload: "hello4" }]);`

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 July 2022 15:28 UTC](https://discourse.nodered.org/t/multiple-messages-with-settimeout/65007/5 "2022-07-12T15:28:34Z")

</div>

Yeah, I should have actually tested it 🤦

---

<div class="post-metadata">

**Author:** ![ninja\_007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ninja_007/32/64510_2.png) [@ninja\_007](https://discourse.nodered.org/u/ninja_007)\
**Post date:** [12 July 2022 15:44 UTC](https://discourse.nodered.org/t/multiple-messages-with-settimeout/65007/6 "2022-07-12T15:44:49Z")

</div>

Thank you , solved

@UnborN thank you for noticing typo

---

<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:** [26 July 2022 15:45 UTC](https://discourse.nodered.org/t/multiple-messages-with-settimeout/65007/7 "2022-07-26T15:45:17Z")

</div>

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