Skip to main content

Squirrel Repeating Timers

Code To Repeatedly Trigger A Supplied Callback

The imp API method imp.wakeup() provides a means to set a timer that will fire after the specified duration and trigger a supplied function. However, it doesn’t provide the option to rerun the timer automatically. This recipe’s code provides a Squirrel class which adds this feature.

You can use the class to instantiate timer objects, providing a duration, whether the timer repeats (true) or not (false), and a callback function. By default, the timer will repeat.

After a non-repeating timer has fired, it is considered invalid — it cannot be used again. Repeating timers can be invalidated by calling their invalidate() method — they will not fire again if this is called, so use it to cancel timers. For a given timer, call its isValid() method to determine its state. This returns a bool.

If you need to trigger a timer early, call its fire() method. This will invalidate non-repeating timers; repeating timers will continue to fire as programmed.

Sample Code

The following code provides a simple class you can include in your own code to add repeating timers. The code after makes uses of this class.

API Methods Used