problem with setInterval() function in node red

843 views
Skip to first unread message

JB

unread,
Apr 24, 2018, 2:34:18 PM4/24/18
to Node-RED
 I am trying to send a payload at intervals which are set to a tempo. I created a global var that divides 60,000 by the number provided by a dashboard slider to obtain the tempo in "beats per minute". This portion is working fine.

However, when I can't get the setInterval function to work. Am I doing something wrong? here is my code:

var tempoMillis = global.get("tempo");
 
function playLoop(){
    return {payload:"start"};
}

if (msg.payload == "start"){
    setInterval(playLoop, tempoMillis);
}

if (msg.payload == "stop"){
    clearInterval();
    return {payload:"stop"};
}

Colin Law

unread,
Apr 24, 2018, 2:51:21 PM4/24/18
to node...@googlegroups.com
You can't use return from setInterval, you should use node.send

Colin

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/813c749d-1d28-4666-a8f7-9c4e3a1867a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nick O'Leary

unread,
Apr 24, 2018, 2:54:48 PM4/24/18
to Node-RED Mailing List
Hi JB,

your code is returning a message from the function setInterval calls. That isn't going to cause anything to be sent. You need to use node.send in order to send a message asynchronously - https://nodered.org/docs/writing-functions#sending-messages-asynchronously

But there's another issue with your code... you have to pass clearInterval the id of the interval timer you want to stop. As it stands, this code will never stop any timer it starts.

You should store the id of the interval you start in global context so you can retrieve it. All together, that means something like this should do it:



var tempoMillis = global.get("tempo");
 
function playLoop(){
    node.send({payload:"start"});
}

if (msg.payload == "start"){
    var id = setInterval(playLoop, tempoMillis);
    global.set("timer",id);
}
if (msg.payload == "stop"){
    clearInterval(global.get("timer"));
    return {payload:"stop"};
}



--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

Dannaz Perth

unread,
Apr 24, 2018, 2:59:12 PM4/24/18
to Node-RED
Nick....

You Beauty mate.......

That did the trick.. Thank you so much fella!.

I hope you have a pleasant week sir.

Dan

JB

unread,
Apr 24, 2018, 3:01:18 PM4/24/18
to Node-RED
Thank you so much, man!... Forgive my newbness. Your code worked and I understand your explanation.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

Colin Law

unread,
Apr 24, 2018, 3:53:04 PM4/24/18
to node...@googlegroups.com
Should the timer not be in node context rather than global?

Colin

JB

unread,
Apr 24, 2018, 7:52:00 PM4/24/18
to Node-RED
Yes thank you.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/813c749d-1d28-4666-a8f7-9c4e3a1867a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
Reply all
Reply to author
Forward
0 new messages