How to convert to a function

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

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

//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

Again from a phone. But you need

clearTimeout(tostopit);

IE clear the reference to the timer that you created for

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

1 Like

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.

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

I think i need to sleep on it

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

//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);

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)

//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

@gbond,

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

sorry dont understand

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

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

@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.

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

the codeing looks exactly the same!

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!

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

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

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

@gbond

What is time_in_ms_to_wait_before_first_execution for?

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

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