Timer Library

The timer library supports scheduling tasks (e.g., animation) through Timer objects.  Timer objects are used to schedule how often to perform a certain task (i.e., how often to call a given function). To use it, you need following in your program:

Creating Timers

Timer objects are used to to schedule functions to be executed after a given time interval, repeatedly or once.  The following function creates a new Timer, so you need to save it in a variable (so you can use it later).

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.

For example, the following:


creates a Timer t, which will call function Play.noteOn(A4) repeatedly every 500 milliseconds (i.e., half second).  In order for a timer to operate, it needs to get started:

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).