Overview
TheTimer class provides a simple mechanism for executing callback functions at regular intervals. It is designed to be updated with elapsed time and will call the callback synchronously when the interval expires.
Constructor
__init__()
Creates a new Timer instance.The timer interval in milliseconds. The callback will be executed each time this interval elapses.
A callable function or method to execute when the interval expires. This function should take no arguments.
If True, the timer will only execute the callback once and then become inactive. If False, the timer will continue to execute the callback at each interval.
Attributes
The Timer instance maintains the following attributes:interval(int): The timer interval in millisecondscallback(callable): The callback function to executeoneshot(bool): Whether this is a one-shot timertime(int): Accumulated time since last callback executionalive(bool): Whether the timer is still active
Methods
update()
Updates the timer with the elapsed time and executes the callback if the interval has expired.The amount of time that has passed since the last call to update(), in milliseconds.
- The callback function is executed
- The accumulated time is reduced by the interval amount
- If
oneshotis True, the timer is marked as inactive (alive = False)