0

I am trying to create a loop in a function node in Node-Red that basically acts like a countdown timer for me. Right now when I run this node I only get a single output from the node with msg.playload saying "Back in 15 minutes".

What I want is to get 15 outputs one after another that say "Back in 15 minutes", "Back in 14 minutes", ect. Once I can get that I will add in a delay so that the for loop waits for 1 minute each time it runs.

var totalMinutes = 15

for (var i = 0; i < 15; i++){
    var minutesLeft = totalMinutes - [i];

    msg.payload = {
        "profile": {
            "status_text": "Back in " + minutesLeft + " minutes",
            "status_emoji": ":coffee:",
            "status_expiration": 0
        }
    };
    return [msg.payload];
}

return [msg];
Nick
  • 63
  • 2
  • 7

1 Answers1

0

you are stopping the code from continuing by using return [msg.payload];. Remove that line and it should print out the way you want it.

Note that the print out is not equivalent to 1 second. To do that you can wrap in a setInterval()

checkout https://www.w3schools.com/js/js_timing.asp for more details.

  • When I do this I only get the results from the last iteration. It shows that I'll be back in 1 minute, but skips 15-2. I want the function node to output 15 different times. – Nick Jul 12 '19 at 20:55
  • 1
    Figured this out. I had to delete the return, but then I also added `node.send(msg);` to the end of my for loop and now I'm getting exactly what I wanted. – Nick Jul 12 '19 at 21:11