Timer Warning a problem?

I wonder if this is a problem when happening:

WARNING: timer.pause() cannot pause a timerId that is already expired.

And if so, how to avoid this? Is there a check if a timer is expired?

This is a very good question… So I got curious.  I wrote a small test program:

local json = require("json") local t1 = timer.performWithDelay(1000, function() print("hello world"); end, 1) print(json.prettify( t1 )) local t2 = timer.performWithDelay(3000, function() print(json.prettify( t1 )); end, 1)

It creates a timer, t1 and immediately dumps the contents of the timer object returned.

{ "\_count":1, "\_listener":"\<type 'function' is not supported by JSON.\>", "\_time":1558.509 }

Then I wait until after the first timer is complete and dump the object a second time.

{ "\_count":2, "\_expired":true, "\_listener":"\<type 'function' is not supported by JSON.\>", "\_removed":true, "\_time":1558.509 }

It seems to me you could test for either t1._expired or t1._removed to see if it’s still running or not:

if not myTimer.\_expired then &nbsp;&nbsp;&nbsp; timer.pause( myTimer ) else &nbsp;&nbsp;&nbsp; -- the timer is done, so house clean if you're done with it. &nbsp;&nbsp;&nbsp; myTimer = nil end

Now of course when we prefix a variable in an object like this with an underscore, it’s meant to be a private variable and private variables are subject to change without warning, but I seriouslly doubt we will be changing the timer library any time soon.

Rob

Thank you for your help here Rob!

This is a very good question… So I got curious.  I wrote a small test program:

local json = require("json") local t1 = timer.performWithDelay(1000, function() print("hello world"); end, 1) print(json.prettify( t1 )) local t2 = timer.performWithDelay(3000, function() print(json.prettify( t1 )); end, 1)

It creates a timer, t1 and immediately dumps the contents of the timer object returned.

{ "\_count":1, "\_listener":"\<type 'function' is not supported by JSON.\>", "\_time":1558.509 }

Then I wait until after the first timer is complete and dump the object a second time.

{ "\_count":2, "\_expired":true, "\_listener":"\<type 'function' is not supported by JSON.\>", "\_removed":true, "\_time":1558.509 }

It seems to me you could test for either t1._expired or t1._removed to see if it’s still running or not:

if not myTimer.\_expired then &nbsp;&nbsp;&nbsp; timer.pause( myTimer ) else &nbsp;&nbsp;&nbsp; -- the timer is done, so house clean if you're done with it. &nbsp;&nbsp;&nbsp; myTimer = nil end

Now of course when we prefix a variable in an object like this with an underscore, it’s meant to be a private variable and private variables are subject to change without warning, but I seriouslly doubt we will be changing the timer library any time soon.

Rob

Thank you for your help here Rob!