NEWS
setTimeout() keeps running on script stop.
-
I have a js-script which sends every 20min the setpoint temperatures to the heating control panels.
Something likes this:let heatingCbTmo = null; function heatingCb() { // do something and send setpoint temps // resart timeout heatingCbTmo = setTimeout(heatingCb, 20 * 60 * 1000); } function main() { // setup heatingCb(); } main();
The problem is, that the timeout keeps running although I have stopped it. If I start the script again (or do an restart), then I have multiple timeout callbacks running! This annoying and not what you expect if you terminate the script. Currently I have 3 timeout instances running.
Is this a desired behavior, or a bug? If it's not a bug, how can I cancel (with cancelTimeout(id)) the timeout on script stop/exit or restart? I need somehow a hook to call cancelTimeout(heatingCbTmo) on exit/stop/restart.
Platform: rapsi4, raspbian (up to date)
Node.js: v16.19.0
NPM: 8.19.3
pi@iobroker:~ $ iob update
Used repository: beta
Adapter "admin" : 6.3.6 , installed 6.3.6
Adapter "backitup" : 2.5.12 , installed 2.5.12
Adapter "discovery" : 3.1.0 , installed 3.1.0
Adapter "enocean" : 0.8.4 , installed 0.8.4
Adapter "homeconnect" : 1.1.1 , installed 1.1.1
Adapter "influxdb" : 3.2.0 , installed 3.2.0
Adapter "jarvis" : 3.1.2 , installed 3.1.2
Adapter "javascript" : 6.1.4 , installed 6.1.4
Controller "js-controller": 4.0.24 , installed 4.0.24
Adapter "rpi2" : 1.3.2 , installed 1.3.2
Adapter "simple-api" : 2.7.2 , installed 2.7.2
Adapter "socketio" : 6.3.3 , installed 6.3.3
Adapter "telegram" : 1.15.2 , installed 1.15.2
Adapter "web" : 5.4.3 , installed 5.4.3
Adapter "ws" : 2.3.5 , installed 2.3.5 -
@j1s2e3 I guess cancelling Timeout ist with
clearTimeout(myTimeout);
not cancelTimeout.
I`m not sure that if you start the script again a new instance is created and you get not the same instance as the timeout was created before. But as i do not work with JS - may someone else can help.
-
@mickym Yes, you are right. It was my fault while writing. In the code I had it correct:
if (heatingCbTmo != null) { clearTimeout(heatingCbTmo); heatingCbTmo = null; }
... and it's working (in subcribe for instance). But the problem is stopping the script. On stop I get this log:
... 12:36:15.857 info javascript.0 (619) script.js.common.VirtualAutomations.04_Heizung: ### setting: room=14-gwc temp=16 12:43:21.268 info javascript.0 (619) Stop script script.js.common.VirtualAutomations.04_Heizung 12:56:13.005 info javascript.0 (619) script.js.common.VirtualAutomations.04_Heizung: ###### before temp_override=0 12:56:13.006 info javascript.0 (619) script.js.common.VirtualAutomations.04_Heizung: ### setting: room=01-die temp=20 ...
so I thought it's not running anymore, including timeouts. But it seems the timeout keeps running...
Now I'm searching for a way to place a hook on or before exit to cancel the timeout with clearTimeout(). No idea so far...
process.on("beforeExit", (code) => {});
process.on does not work. It's not defined.
The first question is, if timeout should keep running if the sarting script is stopped? Whether it's a bug or a feature.
-
@j1s2e3 sagte: The first question is, if timeout should keep running if the sarting script is stopped? Whether it's a bug or a feature.
It's a bug in your javascript instance. Restart the instance or ioBroker.