# Loops inside a node

**URL:** <https://discourse.nodered.org/t/loops-inside-a-node/26402>\
**Category:** General\
**Created:** [10 May 2020 04:20 UTC](https://discourse.nodered.org/t/loops-inside-a-node/26402 "2020-05-10T04:20:41Z")\
**Posts on this page:** 9\
**Page:** 1

<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:** [10 May 2020 04:20 UTC](https://discourse.nodered.org/t/loops-inside-a-node/26402/1 "2020-05-10T04:20:41Z")

</div>

I am delving into loops for some reason.

I send a `payload` (number) and the node spits out that many _messages_.  
(Contents not important)

So for now this is the code:

```auto
let x = msg.payload;
let y = 0;
context.set("COUNT",x); // Not really needed?

while (y < x)
{
    y = y + 1;
    node.warn(y);
// node.send(y); // Not sure on structure. But.....
    delay(1000);
}

```

But when I inject a value, I get this as an error:

```auto
TypeError: Cannot read property 'startsWith' of undefined

```

I've seen this before, but can't remember where and why.

Putting that aside, what am I missing on the loop?

Thanks in advance.

## EDIT

Ok, found first problem in line:

`context.set("COUNT",x);`

Originally I put a `.` rather than a `,`.

But now the `delay` isn't happy.

How do I _delay_?

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [10 May 2020 05:21 UTC](https://discourse.nodered.org/t/loops-inside-a-node/26402/2 "2020-05-10T05:21:16Z")

</div>

> [@Trying\_to\_learn](#):
>
> How do I _delay_ ?

Delay as such is not available in javascript. You have to use `setTimeout` in some way. You could eventually write a function with the name 'delay' but inside, you have to use `setTimeout`

---

<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:** [10 May 2020 05:30 UTC](https://discourse.nodered.org/t/loops-inside-a-node/26402/3 "2020-05-10T05:30:56Z")

</div>

Yeah, I was searching and found `setTimeout` but the docs on it are above my skill set to read and understand. ☹

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [10 May 2020 05:49 UTC](https://discourse.nodered.org/t/loops-inside-a-node/26402/4 "2020-05-10T05:49:15Z")

</div>

```auto
let x = msg.payload;
let y = 0;

function f(x,y) {
    y += 1;
    node.warn(y);
    if(y < x){
        setTimeout( f, 1000, x, y );
    }
}

f(x,y);

```

---

<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:** [10 May 2020 05:53 UTC](https://discourse.nodered.org/t/loops-inside-a-node/26402/5 "2020-05-10T05:53:36Z")

</div>

Thanks.

I still need to learn more. This saved me a lot of mucking about.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [10 May 2020 05:55 UTC](https://discourse.nodered.org/t/loops-inside-a-node/26402/6 "2020-05-10T05:55:14Z")

</div>

Good to hear, I anyway had that code in front of me recently, I then needed a similar delay feature as well 😉

---

<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:** [10 May 2020 08:03 UTC](https://discourse.nodered.org/t/loops-inside-a-node/26402/7 "2020-05-10T08:03:42Z")

</div>

You might be better to split the loop out into a number of nodes and use a Delay node to do the delay.

---

<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:** [10 May 2020 08:07 UTC](https://discourse.nodered.org/t/loops-inside-a-node/26402/8 "2020-05-10T08:07:49Z")

</div>

I do agree @Colin. Alas I was wanting to just optimise the screen size.

Though of course: I could use a `sub flow` node.

It is only for a very small specific use and it is just in testing phase for now.  
I may not use/need it anyway.

It just seems what @krambriw showed me is possibly the _Goldilocks_ code.

---

<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:** [24 May 2020 08:07 UTC](https://discourse.nodered.org/t/loops-inside-a-node/26402/9 "2020-05-24T08:07:52Z")

</div>

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