Correct usage of createTimer

I have read a lot about createTimer being the better way of making your rule wait/sleep for a certain amount of time. However, I saw many codes where a timer is terminated at different places:

  • beginning of script
  • inside timer lambda in combination with if/else
  • end of script

sure, timer termintes automatically. I just wanted to make the script better if it fails at any point so that the timer is not blocking the system
could somebody show me where the ‘correct’ place is in the following code template?

when
   item1 received command
then
   var Timer tTimer = null
   <some code here>
   tTimer = createTimer(now.plusSeconds(20), [ |
      <some code here>
   ])
   <some code here>
   tTimer?.cancel
end

Understand that as used here, tTimer is just a handle or pointer to the independent timer that you create. It does not “contain” the timer, only a pointer.

I think it will be worth re-looking at prior examples in that new light.

Meantime, this rule. Rule starts, you create a variable. Then you create a timer for future execution, and put a handle for that into your tTimer variable.
Then you cancel the timer immediately. Then the rule ends, and the tTimer variable is discarded anyway.

yeah, that’s why I am confused. I have seen this structure quite often here, so I thought it is correct and I need to adapt.
So does your post mean I do not to take care at all about terminating the timer even if (for any reasons) the rule stops because of an error?

I do not understand what you mean by that.

You create a timer. It is either awaiting its appointed time, or it is executing, or it has finished - terminated. You do not have to do anything else.

You might choose to keep a handle at the moment of timer creation. What you do with it after that is up to you. You can use it to communicate with the independent Timer. You can discard it. You can overwrite with “bananas” if you like, that doesn’t affect the timer. It’s up to you how you manage your timer.

What many people do is make their handle global, so that it survives between rule runs.
Set it to what you like, but null is convenient.

Later, when a timer has been created and a handle stored in that global variable, you can test if handle is not-null to see if a timer is already running. Typically you might do that if you wanted to avoid starting a second timer, and/or to reschedule the existing timer.

If you do nothing else to your handle, nothing else happens to it. It does not change in any way whether the Timer is running or long finished. It’s up to you to manage that.

So people will typically code a Timer to set its own handle to null when finished, allowing for easy not-null test. Although you could if you wished set it to “bananas” or “finished”, it’s not a magic word.

If you do choose to use a handle that way, you would need to remember to set it to null again if you should cancel the timer.

Please review examples once again in this light.

1 Like

ok - got it…
that explains a lot of wrong code examples and misunderstandings on my side.
Thank you very much for the time you took to explain this!

1 Like