# Node-red flow -set timeout with js

**URL:** <https://discourse.nodered.org/t/node-red-flow-set-timeout-with-js/37710>\
**Category:** General\
**Created:** [18 December 2020 15:27 UTC](https://discourse.nodered.org/t/node-red-flow-set-timeout-with-js/37710 "2020-12-18T15:27:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [18 December 2020 15:27 UTC](https://discourse.nodered.org/t/node-red-flow-set-timeout-with-js/37710/1 "2020-12-18T15:27:16Z")

</div>

```auto
[{"id":"175bb614.8e64ea","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"2c065ea1.db69e2","type":"function","z":"175bb614.8e64ea","name":"","func":"let counter = 0;\n\nfor (let i = 1; i < 11; i++) {\n \n setTimeout(dimmer, 2000);\n}\n\nfunction dimmer(){\n counter = counter + 10;\n node.send({payload:counter});\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":300,"y":180,"wires":[["864022f5.090eb"]]},{"id":"864022f5.090eb","type":"debug","z":"175bb614.8e64ea","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":180,"wires":[]},{"id":"8d92f9e8.dd5718","type":"inject","z":"175bb614.8e64ea","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":180,"wires":[["2c065ea1.db69e2"]]}]

```

why does node.send not send a message every 3 sec but always all 10 messages at once at the end?

---

<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:** [18 December 2020 15:32 UTC](https://discourse.nodered.org/t/node-red-flow-set-timeout-with-js/37710/2 "2020-12-18T15:32:05Z")

</div>

> [@juntiedt](#):
>
> why does node.send not send a message every 3 sec but always all 10 messages at once at the end

You setup the timeouts like this:

```auto
for (let i = 1; i < 11; i++) {
    setTimeout(dimmer, 2000);
}

```

That means you set up 10 timeouts that all run 2 seconds from the moment that for loop is run - in other words, all 10 timeouts occur at the same time (give or take a few microseconds).

If you want to send 10 messages with 2 seconds between each one, you could do:

```auto
for (let i = 1; i < 11; i++) {
    setTimeout(dimmer, 2000 * i);
}

```

That code sets up the first timeout in 2 seconds, the next timeout in 4 seconds, then 6 seconds and so on.

---

<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:** [18 December 2020 15:44 UTC](https://discourse.nodered.org/t/node-red-flow-set-timeout-with-js/37710/3 "2020-12-18T15:44:37Z")

</div>

A bit of further clarification, the `setTimeout(dimmer, 2000)` call starts the timer and then returns immediately so the loop starts its next iteration straight away, it does not wait for the timer to run down before returning. Then two seconds later `dimmer` will be called.

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [21 December 2020 10:26 UTC](https://discourse.nodered.org/t/node-red-flow-set-timeout-with-js/37710/4 "2020-12-21T10:26:15Z")

</div>

with the help of Johannes (JGKK) I managed to use an enocean rocker 4 switch as a dimmer.  
He did a perfect job!  
Please have a look at this threat:

> [@Node-red-contrib-enocean](https://discourse.nodered.org/t/node-red-contrib-enocean/37409):
>
> I am using the above mentioned Node-red Flow and have some problems with a rocker 4 switch. How can I calculate the time between button pressed and button released. Actually I am not able to identify the resulting messages as coming from the same button. Any help appreciated. regards

---

<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:** [19 February 2021 10:26 UTC](https://discourse.nodered.org/t/node-red-flow-set-timeout-with-js/37710/5 "2021-02-19T10:26:18Z")

</div>

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