Hi
I’ve been trying to make an exponential delay node basically following some function code that I have, but I’ve not built a node before and seem to be struggling I think with managing the context of the node correctly. Basically as further triggers are received the remaining time should be multiplied by the scale factor, but it only seems to set the remaining time to the duration on the first request, and the timer doesn’t seem to reduce this value either.
This is my attempt so far, I’ve tried following some of the other timers and delays, albeit in a much more simplified way: https://github.com/mcinnes01/node-red-contrib-exponential-timer/blob/c39b06c588991967fe45f2b4fb03dad9619f68a6/exponential-timer.js
This is the function I have that does work, there is clearly a difference between managing the state or something between a node and a function.
let timeout = context.get("timer");
let timeoutId = context.get("timeoutId");
clearTimeout(timeoutId);
const min = 10;
const max = 100;
if(msg.reset) {
context.set("timer",null);
node.status({text: 'reset'});
return;
}
if(timeout) {
timeout = Math.min(Math.round(timeout * 1.5), max);
} else {
timeout = min;
}
timeoutId = setTimeout(() => {
context.set("timer",null);
node.status({});
node.send(msg);
node.done();
}, timeout * 1000);
context.set("timer", timeout);
context.set("timeoutId", timeoutId);
node.status({text: timeout});
Thanks in advance for your help
Andy