Skip to content

Timer

Timer objects are created by set_interval or set_timer.

TimerCallback module-attribute

TimerCallback = Union[
    Callable[[], Awaitable[Any]], Callable[[], Any]
]

Type of valid callbacks to be used with timers.

Timer class

def __init__(
    self,
    event_target,
    interval,
    *,
    name=None,
    callback=None,
    repeat=None,
    skip=True,
    pause=False
):

A class to send timer-based events.

Parameters
Parameter Default Description
event_target
MessageTarget
required

The object which will receive the timer events.

interval
float
required

The time between timer events, in seconds.

name
str | None
None

A name to assign the event (for debugging).

callback
TimerCallback | None
None

A optional callback to invoke when the event is handled.

repeat
int | None
None

The number of times to repeat the timer, or None to repeat forever.

skip
bool
True

Enable skipping of scheduled events that couldn't be sent in time.

pause
bool
False

Start the timer paused.

pause method

def pause(self):

Pause the timer.

A paused timer will not send events until it is resumed.

reset method

def reset(self):

Reset the timer, so it starts from the beginning.

resume method

def resume(self):

Resume a paused timer.

stop method

def stop(self):

Stop the timer.