Why does timer:pause() not prevent my timer from finishing?

I start a timer...

refreshTimer = playdate.timer.new(nextRefresh, timedAction)

...now how can I kill it and prevent timedAction?

Neither of the following works (:remove does, see below). The timer still finishes and executes timedAction:

refreshTimer:pause()

refreshTimer = nil --(Just a random experiment)

P.S. I'm sure nothing is RE-starting the timer. I don't have :start anywhere in my code, and the only line of code that creates the timer is followed by a note printed to the console. I also print to the console when I execute the above pause (or nil) to be sure that IS happening. And I log whenever timedAction runs. I get something like this in the console:

Start Timer: 59931.0
TIMEDACTION
Start Timer: 59892.0
Kill Timer
TIMEDACTION

Thanks in advance!

did you try playdate.timer:remove()?

1 Like

Thanks!

I'm still curious why :pause doesn't seem to prevent the timer from finishing (I'll edit my but title). But :remove does the trick! (I had used that early on and forgot—because I needed to be able to unpause it. But happily I no longer need that ability anyway!)

That's a good question.

What about if you do :pause() and then immediately playdate.updateTimers()?

One for @dave I think

2 Likes

The callback shouldn't be called when the timer is paused.

Can you check the value of refreshTimer.paused after you call :pause() and also during each update?

But if you just want to stop the timer and never use it again, you should use remove()
When you call pause() the timer doesn't advance anymore but the timer system still keep track of it until it is removed (even if refreshTimer get assigned a new timer).

1 Like

I did try adding updateTimers on the next line (in addition to the usual place every update). I'm just curious at this point! (If I ever did need to pause and re-start, I could always store the value and then remove/re-create the timer instead.)

I'll experiment with :pause() further later, just to understand. (It seems like a possible bug?)