Using node.status to indicate that a flow has stopped

The aim is to give a visual warning in the editor that a flow has stopped flowing using node.status.
I wrote the code below using setTimeout but it doesn't seem to work as planned, as it displays "STALE" despite the flow being triggered every 5 seconds.
Am I clearing the previous timeout correctly?

function delay(callback, delay) {
  setTimeout(callback, delay);
}

clearTimeout(delay);

node.send({payload: msg.payload})
node.status({ fill: "green", shape: "dot", text: msg.payload + "%" })
delay(function() {
  node.status({ fill: "red", shape: "dot", text: "STALE" })
}, 6000)

Example flow

[{"id":"6803e03ca1cb800f","type":"function","z":"c10377724288bc5e","name":"function 5","func":"function delay(callback, delay) {\n  setTimeout(callback, delay);\n}\n\nclearTimeout(delay);\n\nnode.send({payload: msg.payload})\nnode.status({ fill: \"green\", shape: \"dot\", text: msg.payload + \"%\" })\ndelay(function() {\n  node.status({ fill: \"red\", shape: \"dot\", text: \"STALE\" })\n}, 6000)","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":1460,"wires":[["be5c8a57243cb8bf"]]},{"id":"7a4f70e69da2b462","type":"inject","z":"c10377724288bc5e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$number($floor(1 + $random() * 100))","payloadType":"jsonata","x":510,"y":1460,"wires":[["6803e03ca1cb800f"]]},{"id":"be5c8a57243cb8bf","type":"debug","z":"c10377724288bc5e","name":"debug 43","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":880,"y":1460,"wires":[]}]

Hi @Paul-Reed

I think its just a context thing.

try this..
when i tested , it went stale only after disconnecting the wire and deploying

setTimeout returns and object that you can pass to clearTimeout
but to cancel it, it must be stored in context, given the nature of function nodes

let _timer = context.get("timer")
function timeout(){
    node.status({ fill: "red", shape: "dot", text: "STALE" })
}
if(_timer){
    clearTimeout(_timer);
}

node.send({payload: msg.payload})
node.status({ fill: "green", shape: "dot", text: msg.payload + "%" })

_timer = setTimeout(timeout,6000);
context.set("timer",_timer)

3 Likes

Thanks Marcus, your code works great!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.