Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. English
    3. Scripting / Logic
    4. JavaScript
    5. setTimeout() keeps running on script stop.

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    setTimeout() keeps running on script stop.

    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      j1s2e3 last edited by

      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

      mickym 1 Reply Last reply Reply Quote 0
      • mickym
        mickym Most Active @j1s2e3 last edited by mickym

        @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.

        J 1 Reply Last reply Reply Quote 0
        • J
          j1s2e3 @mickym last edited by

          @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.

          paul53 1 Reply Last reply Reply Quote 0
          • paul53
            paul53 @j1s2e3 last edited by

            @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.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post

            Support us

            ioBroker
            Community Adapters
            Donate

            518
            Online

            31.8k
            Users

            80.0k
            Topics

            1.3m
            Posts

            3
            4
            638
            Loading More Posts
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes
            Reply
            • Reply as topic
            Log in to reply
            Community
            Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
            The ioBroker Community 2014-2023
            logo