# Modify "function node" to create "Loop"

**URL:** https://discourse.nodered.org/t/modify-function-node-to-create-loop/36750
**Category:** General
**Created:** [30 November 2020 10:58 UTC](https://discourse.nodered.org/t/modify-function-node-to-create-loop/36750 "2020-11-30T10:58:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![seyeon](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@seyeon](https://discourse.nodered.org/u/seyeon)
#### Post date: [30 November 2020 10:58 UTC](https://discourse.nodered.org/t/modify-function-node-to-create-loop/36750/1 "2020-11-30T10:58:22Z")

</div>

![스크린샷 2020-11-30 오후 8.05.47](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/7/b769c74314d0ec05c067a940e300e0181283216c.png)

```auto
[{"id":"7401e57d.659e4c","type":"function","z":"f5d4feda.70164","name":"dimming","func":"x = msg.payload\nfor(var x=50; x<=255; x++)\n{\n node.send({\"bright\":x})\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":260,"wires":[["4bb6c99b.16b0b8"]]},{"id":"4bb6c99b.16b0b8","type":"delay","z":"f5d4feda.70164","name":"","pauseType":"rate","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"0.05","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":670,"y":260,"wires":[["f888b1de.2446a"]]},{"id":"f888b1de.2446a","type":"change","z":"f5d4feda.70164","name":"bright to payload","rules":[{"t":"move","p":"bright","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":890,"y":260,"wires":[["451189b4.7844f8"]]},{"id":"451189b4.7844f8","type":"debug","z":"f5d4feda.70164","name":"bright","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1090,"y":260,"wires":[]},{"id":"dd723f23.5e6c3","type":"inject","z":"f5d4feda.70164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"50","payloadType":"num","x":290,"y":260,"wires":[["7401e57d.659e4c"]]},{"id":"413d0494.b8896c","type":"function","z":"f5d4feda.70164","name":"dimming","func":"x = msg.payload\nfor(var x=255; x>=50; x--)\n{\n node.send({\"bright\":x})\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":320,"wires":[["4bb6c99b.16b0b8"]]},{"id":"f15efa87.c3bb28","type":"inject","z":"f5d4feda.70164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"255","payloadType":"num","x":290,"y":320,"wires":[["413d0494.b8896c"]]}]

```

There are two inject node(50, 255).  
When "50" is entered,

> x = msg.payload  
> for(var x=50; x\<=255; x++)  
> {  
> node.send({"bright":x})  
> }

When "255" is entered,

> x = msg.payload  
> for(var x=255; x\>=50; x--)  
> {  
> node.send({"bright":x})  
> }

Function node activates.

**I want to repeat the last message on the node until the other data comes in.**  
**(For example, input "50" -\> function node ( x=50; x\<=255; x++ ) -\> Repeat "255"(last message) indefinitely until new "255" is entered.)**

Which node should I add? loop? repeat?  
Or how should I modify the function node?

---

<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: [30 November 2020 11:25 UTC](https://discourse.nodered.org/t/modify-function-node-to-create-loop/36750/2 "2020-11-30T11:25:25Z")

</div>

If you just want to keep repeating the previous message then you can do that with a Trigger node configured like this.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/4/9434032f9eeab57412a81fd2aa9979cc30e4b21f.png)

---

<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: [14 December 2020 11:25 UTC](https://discourse.nodered.org/t/modify-function-node-to-create-loop/36750/3 "2020-12-14T11:25:37Z")

</div>

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