Timer Library

The timer library supports scheduling tasks (e.g., animation) through Timer objects.

To use timers, you need following in your program:

from timer import *


Timer objects schedule how often to perform a task (i.e., how often to call a function), and specify if the execution will be done once or repeatedly (using a time interval). For example, the following:

t = Timer(500, Play.noteOn, [A4], True)

creates a Timer t, which every 500 milliseconds (i.e., half second) calls function Play.noteOn(A4) repeatedly.  Notice that the parameters to the function as provided as a separate list (i.e., [A4] above).

In order for a timer to operate, it needs to get started:



Function Description
Timer(delay, function, parameters, repeat) Creates a new Timer to execute function after delay time interval (in milliseconds).  The optional parameter parameters is a list of parameters to pass to the function (when called).  The optional parameter repeat (boolean – default is True) determines if the timer will go on indefinitely.NOTE:  The list of parameters is fixed at timer creation time and cannot be modified.

Once a Timer t has been created, the following functions are available:

Function Description
t.start() Starts timer t.
t.stop() Stops timer t.
t.getDelay() Returns the delay time interval of timer t (in milliseconds).
t.setDelay(delay) Sets a new delay time interval for timer t (in milliseconds).  This allows to change the speed of the animation, after some event occurs.
t.isRunning() Returns True if timer t is running (has been started), False otherwise.
t.setFunction(function, parameters) Sets the function to execute.  The optional parameter parameters is a list of parameters to pass to the function (when called).
t.getRepeat() Returns True if timer t is set to repeat, False otherwise.
t.setRepeat(flag) If flag is True, timer t is set to repeat (this also starts the timer, if stopped).  Otherwise, if flag is False, timer t is set to not repeat (this stops the timer, if running).