# Count down function

**URL:** <https://discourse.nodered.org/t/count-down-function/24348>\
**Category:** General\
**Created:** [6 April 2020 19:07 UTC](https://discourse.nodered.org/t/count-down-function/24348 "2020-04-06T19:07:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![koos147](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/koos147/32/19494_2.png) [@koos147](https://discourse.nodered.org/u/koos147)\
**Post date:** [6 April 2020 19:07 UTC](https://discourse.nodered.org/t/count-down-function/24348/1 "2020-04-06T19:07:20Z")

</div>

Good day,

I expect that i made a realy simple mistake.

The idea is that when a message arrive the output count down.

```auto
var i = 10;
while (i >= 5) {
msg.payload=i;
node.send(msg);
  i--;
}

```

Expected output

```auto
10
9
8
7
6
5

```

However the number of outputs is correct. but the payload is wrong.

```auto
10
5
5
5
5
5

```

What do i do wrong?

---

<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:** [6 April 2020 19:16 UTC](https://discourse.nodered.org/t/count-down-function/24348/2 "2020-04-06T19:16:09Z")

</div>

Try a for function.

```auto
for(i=10;i>4;i--){
    node.send({payload:i});
}
node.done();
return;

```

Johannes

---

<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:** [6 April 2020 19:23 UTC](https://discourse.nodered.org/t/count-down-function/24348/3 "2020-04-06T19:23:28Z")

</div>

actually i don’t see anything wrong with the while loop either as it works for me:

```auto
let i = 10;
while(i >= 5){
    msg.payload = i;
    node.send(msg);
    i--;
}
node.done();
return;

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/f/5f22b192a1844fc25a670cef32ca4c726ef4c8c3.jpeg)

---

<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:** [6 April 2020 19:41 UTC](https://discourse.nodered.org/t/count-down-function/24348/4 "2020-04-06T19:41:05Z")

</div>

My guess is the op is using node-red pre version 1

Message is getting overwritten in the loop before debug output.

In later version, message is cloned by default to handle the new asynchronous nature of version 1

> **[Cloning messages in a flow](https://nodered.org/blog/2019/09/13/cloning-messages)**
>
> With the move to asynchronous messaging, we're also changing how the Function node clones messages. Find out what cloning is all about, why it's necessary and what's changing in 1.0.

---

<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:** [6 April 2020 20:39 UTC](https://discourse.nodered.org/t/count-down-function/24348/5 "2020-04-06T20:39:27Z")

</div>

I think you are missing the whole `context` part.

The variable `i` is not honoured between messages.

So the code would have to look (something) like:

```auto
if (context.get("counter) > 0
{
     let i = context.get("counter");
} else
{
    context.set("counter",10);
}
while (i > 5)
{
    msg.payload = 1;
    i = i - 1;
    context.set("counter",1);
    return msg;
}

```

---

<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:** [6 April 2020 20:52 UTC](https://discourse.nodered.org/t/count-down-function/24348/6 "2020-04-06T20:52:21Z")

</div>

This doesn’t work what your proposing. The first part will will only get executed when a new message arrives and the while loop will only go once because you return out of it.  
He needs to use the node.send() function as sending messages from a loop is asynchronous work.  
This will count down one by only when new messages arrive and the while loop has no function at all in your variant.

---

<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:** [6 April 2020 20:54 UTC](https://discourse.nodered.org/t/count-down-function/24348/7 "2020-04-06T20:54:57Z")

</div>

Yeah, ok.

I am just now learning about this `asynchronous` stuff and reading about `node.send( )`.

Sorry for the incorrect reply.

I am going to have to read up on what this _new_ thing is. Though it has been around since NR 1.x onwards.

And I think I also didn't quite get what was meant anyway.

---

<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 April 2020 21:09 UTC](https://discourse.nodered.org/t/count-down-function/24348/8 "2020-04-06T21:09:51Z")

</div>

Another offering!  
This time with a optional delay built into the countdown.

```auto
[{"id":"fbf79e8f.4b4f9","type":"function","z":"4487e413.bb781c","name":"Countdown","func":"var counter = 10;\n\nvar countdown = setInterval(function(){\n node.send({\"payload\":(counter)});\n counter--;\n\n if (counter <5) {\n clearInterval(countdown);\n }\n}, 1000);","outputs":1,"noerr":0,"x":380,"y":990,"wires":[["773a223c.2189ac"]],"icon":"font-awesome/fa-hourglass-1"},{"id":"3a80d1cb.a423de","type":"inject","z":"4487e413.bb781c","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":210,"y":990,"wires":[["fbf79e8f.4b4f9"]],"icon":"node-red/arrow-in.svg"},{"id":"773a223c.2189ac","type":"debug","z":"4487e413.bb781c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":560,"y":990,"wires":[]}]

```

Edit// flow updated

---

<div class="post-metadata">

**Author:** ![koos147](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/koos147/32/19494_2.png) [@koos147](https://discourse.nodered.org/u/koos147)\
**Post date:** [7 April 2020 08:33 UTC](https://discourse.nodered.org/t/count-down-function/24348/9 "2020-04-07T08:33:03Z")

</div>

Good day all,

Thank you all for the effort.

the version installed is 0.15.2 (i think i need to do some updating 😉 )  
I tested the version of Paul-Reed and that does exactly what i need.

the current version is 5 nodes and i can finaly replace them.

 ![count](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/b/7b36601d5e27eca4a7a2989701c4fe6cf8323748.png)

Again many thanks for your quick help.

---

<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:** [21 April 2020 08:33 UTC](https://discourse.nodered.org/t/count-down-function/24348/10 "2020-04-21T08:33:06Z")

</div>

Closed on 21 April 2020 08:33 UTC

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