# 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:** 3

<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:43 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/41 "2018-12-28T23:43:09Z")

</div>

that is ok when ever you r able to will be fine sorry its beyond me at the moment but what I have so far is great

so much easier to play around with the timings

---

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

</div>

I'm just looking for a pointer. Not looking for you to fix it knolleary. here is where im at.

```auto
//get the values of msg.* to var's
var numberofblinks = msg.numberofblinks;
var light_on_for_ms = msg.lighton;
var light_off_for_ms = msg.lightoff;
//var run;

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

//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 = 1;

//function to count the number of blinks and stop timmer
function blinks()
{
    if (numberofblinks < count)
    {
        //stop the recursive setTimeout
        clearTimeout(tostopit);
    
    }
    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
    var tostopit = setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);
}, time_in_ms_to_wait_before_first_execution);

return msg;

```

it now gives me "ReferenceError: tostopit is not defined" so im poking at the right spot

---

<div class="post-metadata">

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

</div>

Again from a phone. But you need

clearTimeout(tostopit);

IE clear the reference to the timer that you created for

---

<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:46 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/44 "2018-12-28T23:46:31Z")

</div>

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

little example of how I am using this so far (so much tider)

---

<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:58 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/45 "2018-12-28T23:58:05Z")

</div>

> [@meeki007](#):
>
> var tostopit

You define `tostopit` inside the run function, so it isn't in scope at the point you call `clearTimeout`.

Stick `var tostopit;` at the top of the function and remove the `var` keyword where you set it's value inside the run function.

---

<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:** [29 December 2018 00:01 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/46 "2018-12-29T00:01:22Z")

</div>

Good Idea: I've done that.......Just did it again .......  
It runs forever without stopping 🙂

I think i need to sleep on it

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [29 December 2018 09:22 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/47 "2018-12-29T09:22:00Z")

</div>

Move `blinks()` to the end of the `setTimeout(function run()`

```auto
//run the light show!!!!
setTimeout(function run() 
{
    //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);
    
    //total ammount of time light can be on or off before it runs again
    tostopit = setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);
    
    blinks();
    
}, time_in_ms_to_wait_before_first_execution);

```

---

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

</div>

this has got rid off the error,  
but if i run it once looks ok  
but second time the light stays switch on to start with and stays on longer than it should before starting to flash (dont get the exactly same sequence each time)

---

<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:** [29 December 2018 12:35 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/49 "2018-12-29T12:35:45Z")

</div>

//get the values of msg.\* to var's  
var numberofblinks = msg.numberofblinks;  
var light\_on\_for\_ms = msg.lighton;  
var light\_off\_for\_ms = msg.lightoff;  
//var run;

//how long to wait before starting the light show  
var time\_in\_ms\_to\_wait\_before\_first\_execution = 1000;

//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 1  
var count = 1;

```
var tostopit;

```

//function to count the number of blinks and stop timmer  
function blinks()  
{  
if (numberofblinks \<= count)  
{  
//stop the recursive setTimeout  
clearTimeout(tostopit);

```
}
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()  
{  
//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);

//total ammount of time light can be on or off before it runs again
tostopit = setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);

blinks();

```

}, time\_in\_ms\_to\_wait\_before\_first\_execution);

return msg;

code i am using so far

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [29 December 2018 12:39 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/50 "2018-12-29T12:39:55Z")

</div>

@gbond,

To simplify code copy add a line with ``` at the begin and at the end of your code.

---

<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:** [29 December 2018 12:48 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/51 "2018-12-29T12:48:41Z")

</div>

sorry dont understand

---

<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:** [29 December 2018 12:49 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/52 "2018-12-29T12:49:14Z")

</div>

also the timing seem all over the place now seem to work better with the error

---

<div class="post-metadata">

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

</div>

> [@gbond](#):
>
> sorry dont understand

see [https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<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:** [29 December 2018 12:55 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/55 "2018-12-29T12:55:15Z")

</div>

@gbond when you share code or flows in this forum, it helps to format it properly so it displays clearly. The post @cflurin has linked to (and to which I pointed you at much earlier on in this thread) describes how to do it.

---

<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:** [29 December 2018 12:58 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/56 "2018-12-29T12:58:12Z")

</div>

ok read your post but i dont see what has changed am i missing some thing?

the codeing looks exactly the same!

---

<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:** [29 December 2018 13:16 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/58 "2018-12-29T13:16:51Z")

</div>

That's because you have not edited your post above, and formatted it as per the requests.  
If you want us to help you, you have to help us a little and format your code!

---

<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:** [29 December 2018 13:18 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/59 "2018-12-29T13:18:19Z")

</div>

I think i have worked it out! "do you mean its lost it's colouring" sorry for being so slow

---

<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:** [29 December 2018 13:20 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/60 "2018-12-29T13:20:00Z")

</div>

```auto
//get the values of msg.* to var's
var numberofblinks = msg.numberofblinks;
var light_on_for_ms = msg.lighton;
var light_off_for_ms = msg.lightoff;
//var run;

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

//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 1
var count = 1;

    var tostopit;

//function to count the number of blinks and stop timmer
function blinks()
{
    if (numberofblinks <= count)
    {
        //stop the recursive setTimeout
        clearTimeout(tostopit);
    
    }
    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() 
{
    //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);
    
    //total ammount of time light can be on or off before it runs again
    tostopit = setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);
    
    blinks();
    
}, time_in_ms_to_wait_before_first_execution);

return msg;

```

---

<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:** [29 December 2018 13:27 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/61 "2018-12-29T13:27:13Z")

</div>

I think you code is right I think you have put a delay start that made the timing seem different

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [29 December 2018 13:43 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/62 "2018-12-29T13:43:14Z")

</div>

@gbond

What is `time_in_ms_to_wait_before_first_execution` for?

I've removed it and changed the "style" a little bit:

```auto
//get the values of msg.* to var's
var numberofblinks = msg.numberofblinks;
var light_on_for_ms = msg.lighton;
var light_off_for_ms = msg.lightoff;

//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 1
var count = 1;

var tostopit;

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

//function to set payload to true
function payload_true() {
    //set payload to true
    msg.payload = true;
    node.send(msg);

    //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
    msg.payload = false;
    node.send(msg);

    //clear status when payload false
    node.status({});
}

//run the light show!!!!
setTimeout(function run() {
    //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);

    //total ammount of time light can be on or off before it runs again
    tostopit = setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);

    blinks();

}, 0);

return null;

```

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

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