# How to convert to a function

**URL:** <https://discourse.nodered.org/t/how-to-convert-to-a-function/6170>\
**Category:** General\
**Created:** [27 December 2018 16:49 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170 "2018-12-27T16:49:05Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [27 December 2018 23:08 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/21 "2018-12-27T23:08:10Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3e7fdd910b9bd5e510ccd6fccfb81d5215f81a35.png)  
Hope this is clearer  
Want to be able to reduce or multiple this bit with the ability of adjusting the time values in one go (hope that make sense)

also just noted I already have one of the delay set different to the others another reason to try & replace with varibles

---

<div class="post-metadata">

**Author:** ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)\
**Post date:** [27 December 2018 23:49 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/22 "2018-12-27T23:49:21Z")

</div>

It's not exactly converting to a function, but the [node-red-contrib-mytimeout](https://www.npmjs.com/package/node-red-contrib-mytimeout) node accepts overrides if you feed it JSON formatted payloads. You could store your variable delays as flow variables and build your desired payload using a template node.

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [27 December 2018 23:56 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/23 "2018-12-27T23:56:06Z")

</div>

![20181227_232152_Trim](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d15de590ead7bab22dda5063e88451f8238bf779.gif)  
this is what its for just want to be able to change easerly

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [28 December 2018 00:02 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/24 "2018-12-28T00:02:46Z")

</div>

I am sure you are right but that looks way beyond my knowledge

---

<div class="post-metadata">

**Author:** ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)\
**Post date:** [28 December 2018 00:10 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/25 "2018-12-28T00:10:47Z")

</div>

> [@gbond](#):
>
> that looks way beyond my knowledge

You don't get better by sticking with what's comfortable. Play with it some and ask specific questions when you hit roadblocks. It's what the community's for.

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [28 December 2018 16:53 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/26 "2018-12-28T16:53:59Z")

</div>

Got a function working for ya. If you want to expand it id start with error checks and not allowing it to be triggered multiple times before it finishes its blink show.

You send it:  
msg.lighton a numerical value in ms you want the light to be on  
msg.lightoff a numerical value in ms you want the light to be off after it was on  
msg.numberofblinks a numerical value of the number of time you want it to blink at that rate.

 ![thelightshow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f7318a9cf73c9aaeda92df87f4ece8c1735cb3fb.png)

```auto
[{"id":"fd93f008.0e423","type":"change","z":"41f9ead2.0eab04","name":"","rules":[{"t":"set","p":"lighton","pt":"msg","to":"250","tot":"num"},{"t":"set","p":"lightoff","pt":"msg","to":"1000","tot":"num"},{"t":"set","p":"numberofblinks","pt":"msg","to":"3","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":260,"wires":[["1871a0b2.ea84af"]]},{"id":"3a5aa846.ad23c8","type":"inject","z":"41f9ead2.0eab04","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":260,"wires":[["fd93f008.0e423"]]},{"id":"7b2a537a.4ba75c","type":"debug","z":"41f9ead2.0eab04","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":730,"y":260,"wires":[]},{"id":"1871a0b2.ea84af","type":"function","z":"41f9ead2.0eab04","name":"The Light Show","func":"//get the values of msg.* to var's\nvar numberofblinks = msg.numberofblinks;\nvar light_on_for_ms = msg.lighton;\nvar light_off_for_ms = msg.lightoff;\n\n//how long to wait before starting the light show\nvar time_in_ms_to_wait_before_first_execution = 0;\n\n//total run time of light on and light off\nvar time_in_ms_to_wait_between_recursive_setTimeout = (light_on_for_ms + light_off_for_ms);\n\n//set the count to 0\nvar count = 0;\n\n//function to count the number of blinks and stop timmer\nfunction blinks()\n{\n if (count === numberofblinks)\n {\n //stop the recursive setTimeout\n clearTimeout(run);\n \n \n }\n else\n {\n count++;\n }\n}\n\n\n\n//function to set payload to true\nfunction payload_true()\n{\n //set payload to true\n node.send({payload:true});\n \n //display green dot when light is on\n node.status({\n fill: 'green',\n shape: 'dot',\n text: ' '\n });\n}\n\n//function to set payload to false\nfunction payload_false()\n{\n //set payload to false\n node.send({payload:false});\n \n //clear status when payload false\n node.status({\n });\n}\n\n//run the light show!!!!\nsetTimeout(function run() \n{\n blinks();\n //turn the light on by setting msg.payload to true\n payload_true();\n \n //turn the light off by waiting to set the payload to false by the ammount of time light is on\n setTimeout(payload_false, light_on_for_ms);\n //i++;\n \n //total ammount of time light can be on or off before it runs again\n setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);\n}, time_in_ms_to_wait_before_first_execution);\n\n\n//clearTimeout(run);\nreturn msg;\n","outputs":1,"noerr":0,"x":560,"y":260,"wires":[["7b2a537a.4ba75c"]]},{"id":"9492094c.fcb338","type":"comment","z":"41f9ead2.0eab04","name":"every thing is in milliseconds","info":"","x":220,"y":180,"wires":[]},{"id":"9510cdff.f4ea8","type":"comment","z":"41f9ead2.0eab04","name":"make sure all msg values are set to numbers","info":"","x":270,"y":220,"wires":[]}]

```

the JS script in the function:

```auto
var numberofblinks = msg.numberofblinks;
var light_on_for_ms = msg.lighton;
var light_off_for_ms = msg.lightoff;

//how long to wait before starting the light show
var time_in_ms_to_wait_before_first_execution = 0;

//total run time of light on and light off
var time_in_ms_to_wait_between_recursive_setTimeout = (light_on_for_ms + light_off_for_ms);

//set the count to 0
var count = 0;

//function to count the number of blinks and stop timmer
function blinks()
{
    if (count === numberofblinks)
    {
        //stop the recursive setTimeout
        clearTimeout(run);
        
        
    }
    else
    {
        count++;
    }
}

//function to set payload to true
function payload_true()
{
    //set payload to true
    node.send({payload:true});
    
    //display green dot when light is on
    node.status({
                    fill: 'green',
                    shape: 'dot',
                    text: ' '
                });
}

//function to set payload to false
function payload_false()
{
    //set payload to false
    node.send({payload:false});
    
    //clear status when payload false
    node.status({
            });
}

//run the light show!!!!
setTimeout(function run() 
{
    blinks();
    //turn the light on by setting msg.payload to true
    payload_true();
    
    //turn the light off by waiting to set the payload to false by the ammount of time light is on
    setTimeout(payload_false, light_on_for_ms);
    //i++;
    
    //total ammount of time light can be on or off before it runs again
    setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);
}, time_in_ms_to_wait_before_first_execution);

//clearTimeout(run);
return msg;

```

hope this helps

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [28 December 2018 17:35 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/27 "2018-12-28T17:35:52Z")

</div>

Thanks

I will take a look asap and let u know how I get on

Garrath

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [28 December 2018 17:59 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/28 "2018-12-28T17:59:29Z")

</div>

Had a quick go

Some off it appears to work but I am getting this error

28/12/2018, 17:57:32[node: The Light Show](http://bonddea.co.uk:1882/)function : (error)

"ReferenceError: run is not defined"

![](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex026/uploads/nodered/original/2X/5/52d5f1c08649212fe6768d2cacdcf8df711810d9.jpeg)

![](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex026/uploads/nodered/original/2X/c/cc3f90e2d51d14117455e0000759b4086072762c.jpeg)

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [28 December 2018 18:16 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/29 "2018-12-28T18:16:31Z")

</div>

that is because the 'run' is ended with clearTimeout(run);

you can ignore this or write some error handling in if you wish.

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [28 December 2018 18:20 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/30 "2018-12-28T18:20:06Z")

</div>

the idea behind writing this function was to annotate it the best I could and let your expand and learn. If you need things like error checking, msg handling, things like: is it numerical; you need to add it yourself.

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [28 December 2018 18:23 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/31 "2018-12-28T18:23:15Z")

</div>

thats great it seems to work and is way better than how i was doing it

many thanks

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [28 December 2018 18:29 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/32 "2018-12-28T18:29:53Z")

</div>

> [@meeki007](#):
>
> that is because the 'run' is ended with clearTimeout(run);

could you explain this bit a little more, sorry I dont understand

---

<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:** [28 December 2018 18:39 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/33 "2018-12-28T18:39:07Z")

</div>

> [@meeki007](#):
>
> that is because the 'run' is ended with clearTimeout(run);

That isn't how you clear timeouts. The call to setTimeout returns a timer object. You must pass _that_ object to clearTimeout, not the function.

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [28 December 2018 19:13 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/34 "2018-12-28T19:13:25Z")

</div>

sorry have no idea what you are talking about

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [28 December 2018 19:17 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/35 "2018-12-28T19:17:38Z")

</div>

I got It kinda.........because setTimeout is being referenced outside the function there must be a reference to it.

basically im doing it wrong. 🙂 and the code works 😛

this is my first go at a timer. so my brain is stretched to the max

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [28 December 2018 19:55 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/36 "2018-12-28T19:55:06Z")

</div>

Well I'm going to need help fixing it. I don't think i have the knowledge.

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [28 December 2018 19:58 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/37 "2018-12-28T19:58:16Z")

</div>

Thanks anyway as it does work but I cant see how to get rid of the errors either (I understand some of it)

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [28 December 2018 21:42 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/38 "2018-12-28T21:42:42Z")

</div>

so is this what i need todo?

WRONG?  
setTimeout(run, time\_in\_ms\_to\_wait\_between\_recursive\_setTimeout);

CORRECT?  
var tostopit = setTimeout(run, time\_in\_ms\_to\_wait\_between\_recursive\_setTimeout);

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [28 December 2018 23:26 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/39 "2018-12-28T23:26:48Z")

</div>

```
//total ammount of time light can be on or off before it runs again

```

// setTimeout(run, time\_in\_ms\_to\_wait\_between\_recursive\_setTimeout);  
var tostopit = setTimeout(run, time\_in\_ms\_to\_wait\_between\_recursive\_setTimeout);

I still get this error :- 28/12/2018, 23:26:09[node: The Light Show](http://bonddea.co.uk:1882/#)function : (error)

"ReferenceError: run is not defined"

---

<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:** [28 December 2018 23:40 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/40 "2018-12-28T23:40:37Z")

</div>

@gbond that is not a complete fix for the function - just one part of what would be needed. Unfortunately I'm in no position to provide a full code example from my phone.

[Previous page](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170.md?page=1)

[Next page](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170.md?page=3)
