Timer 2.0 Library for Corona SDK

I recently commented on a post on the Corona Labs forum from a developer who was having a hard time with timers that were not canceled when a scene was changed. Now the Corona SDK already gives us methods for pausing, resuming, or even canceling active timers, but those methods are somewhat restricted in that you can only cancel one timer at a time, and you must refer to the timer by it’s handle to do so. Under the current system, it’s very easy to accidentally leave a timer running when you would rather pause or cancel all your timers, or at least a number of them.

In my comment on that forum post, I suggested that the original poster vote up a Corona SDK feature request for the timer library to receive an update akin to the “Transition 2.0” update from a few years ago, so that we would have the ability to pause/resume/cancel all active timers at once, or to pause/resume/cancel a subset of active timers by tagging them. I still think that’s worth doing, and if you’re reading this, then you should definitely do that! However, part of what I love about Corona is how we developers can roll our own solutions to these sorts of problems while we wait for Corona Labs to add new features or fix bugs – so I decided to do just that and “fix” the timer library to enable those requested features.

How To Use The Timer 2.0 Library

Updating the timer library to “timer 2.0” in your Corona project is as simple as these two steps:

Require the library in your project by adding this one line of code, preferably near the top of your main.lua:

Lua

1

require("timer2")

By taking those steps, you have updated Corona’s built-in timer library to version 2.0. Note that your existing code will continue to work – version 2.0 is fully backwards-compatible with the current official timer APIs. But you also gain access to these new features:

tag is a string that appends a tag to the created timer that can be used to pause, resume, or cancel all timers with the same tag at once. Defaults to nil.

exclude is a boolean (true/false) parameter that, when set to true, excludes the timer from being impacted by timer.pause(), timer.resume(), or timer.cancel() calls that do not specifically target it by passing the timer’s handle or tag. Timers that are mission-critical and should always run should have their exclude parameter set to true. Defaults to false.

These new arguments can be added in any order to the end of any existing timer.performWithDelay() call.

As an example, the following code would create a timer with the tag “myTag” that is excluded from non-specific pause, resume, or cancel calls:

Lua

1

timer.performWithDelay(1000,myFunction,1,"myTag",true)

timer.pause(), timer.resume(), and timer.cancel() still work as before, but with the following new features:

You can pause, resume, or cancel all active timers (unless they have their exclude parameter set to true) by calling timer.pause(), timer.resume(), or timer.cancel() with no arguments.

You can pause, resume, or cancel all timers with a specific tag by passing the tag as a parameter. For example, the following code would pause all active timers with the tag “myTag”.

Lua

1

timer.pause("myTag")

I hope that my fellow Corona developers will find this unofficial library update to be useful. As with all Corona code I post on this site, the library is free to download, free to use, and you are free to modify it as you see fit. If you do make improvements you think others could use, please share in the comments, or contact me so I can update the library here on the site. Enjoy!

And more, I think you should discriminate the “tag” and “timerID” in timer.pause, timer.resume, timer.cancel.
Tag must be a string while timerID is a userdata ( table ). Make it clear is better :).
Sorry for my bad English 🙁

Thanks for commenting! Yikes, I left a couple of print statements in the module from when I was testing it out, and yes – if you don’t pass an argument to timer.pause(), you’ll get a Runtime error because I’m trying to print the argument and forgot to convert it to a string. My bad! Those couple of print statements are actually not required, so I’ve removed them and updated the module for download. If you re-download it, you should be good to go. Or, if you want, just remove lines 101 & 104 from the old version. Thanks for catching my mistake!

First, thanks for your code, really helped out and solved a number of issues.

I did find that it didn’t quite perform as expected when I specified iterations greater than one and paused the timer between iterations, I found it would delete the timer so I could not resume (timeLeft was less than 0 for that iteration).

I modified your code slightly by modifying the line as follows, seems to work.

Craig’s fix has been incorporated into the module, which you can re-download and use (thanks, Craig!). The download link above now goes to the the timer2 repository on GitHub (I’m going to be distributing my modules via GitHub from now on). Thanks! 🙂

Just dropping by to say thank you for the code!
This has just saved me quite a headache by not having to go back through all my code adding tags to pause timers – is rather efficient and really useful!
Makes me wonder why they do not update Corona simply to work this way.
Anyway, thanks again, and all the best to you.
Warm regards,
Nick

Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Website

Please Do Not Feed The Developers

The Corona SDK tutorials, modules and code snippets that I post on this site are free for you to use, with no expectation of repayment. But a few kind souls have told me that they wanted to toss me a few bucks as a way of saying "thanks," so I've added this donation button. If you like what I'm doing here, and wanted to contribute, feel free. And if not, that's cool too. I'm just glad you're here.