Need help on setTimeout And ClearTimeout

467 views
Skip to first unread message

Thomas Bruyere

unread,
Feb 28, 2017, 6:30:32 PM2/28/17
to Node-RED
Hi, I need your help to use setTimeout() and particulary clearTimeout().
Here is my code :

var msgEntree = msg.payload;
var msg2 = { payload: null };
var finMills = (6 - msg.payload.temps) * 60000;
var tempsMills = msg.payload.temps * 60000;

context
.T1 = null;
context
.T2 = null;

if (msg.payload == "reset"){
    msg
.payload = {action : "eteint", pieceId : 0, lampeId : 0};
        node
.status({});
            clearTimeout
(context.T1);
            clearTimeout
(context.T2);
            context
.T1 = null;
            context
.T2 = null;
    node
.send(msg);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
if (msgEntree.debutFin == "0"){
   
//node.send(msg);
   
//visuel
    node
.status({fill:"red",shape:"dot",text:msgEntree.temps + " min ON"});
   
//passage du message pour allumage direct, on dans le cas d'un demarrage "debut" de periode
    msg
.payload = msgEntree;
    node
.send(msg);
// extinction au bout du temps

    context
.T1 = setTimeout(function(){
   
// visuel
        node
.status({fill:"green",shape:"dot",text:(360000 - tempsMills)/60000 + " min OFF"});

   
//extinction au bout du temps
        msgEntree
.action = "eteint";
        msg
.payload = msgEntree;
        node
.send(msg);
        context
.T1 = null;
   
// redemarrage du random au bout d'une heure

            context
.T2 = setTimeout(function(){
                msg
.payload = "absent";
                node
.send([null,msg]);
                    context
.T2 = null;
                    clearTimeout
(context.T1);
                    clearTimeout
(context.T2);
                node
.status({});        
           
},360000 - tempsMills);
}, tempsMills);
///////////////////////////////////////////////////////////////////////////////////////
} else if (msgEntree.debutFin == "1"){
   
//visuel
    node
.status({fill:"blue",shape:"dot",text:finMills / 60000 + " min OFF"});

    context
.T1 = setTimeout(function(){
       
//visuel
        node
.status({fill:"grey",shape:"dot",text:(360000 - finMills)/60000 + " min ON"});
        msg
.payload = msgEntree;
        node
.send(msg);
        context
.T1 = null;
            context
.T2 = setTimeout(function(){
                msg
.payload = {action : "eteint", pieceId : 0, lampeId : 0};
                msg2
.payload = "absent";
                node
.send([msg,msg2]);
                        context
.T2 = null;
                        clearTimeout
(context.T1);
                        clearTimeout
(context.T2);
                    node
.status({});
           
},360000 - finMills);
   
},finMills);
}
////////////////////////////////////////////////////////////////////////////////////////////0
else {node.status({});}

I have tried a lot of things but the twice timeout never cancel.
Can you help me please to understand how it works.

Thx
thomas

Nathanaël Lécaudé

unread,
Feb 28, 2017, 7:22:58 PM2/28/17
to Node-RED
Seems like you are getting rid of the timeout object before clearing it:
                    context.T2 = null;
                    clearTimeout
(context.T1);
                    clearTimeout
(context.T2);

context.T2 = null; should go after the clearTimeouts

Thomas Bruyere

unread,
Mar 2, 2017, 2:10:33 AM3/2/17
to Node-RED
Really thx, it works!!
Thomas
Reply all
Reply to author
Forward
0 new messages