Download Max Tutorial Manual - Electronic Art and Design

Transcript
Tasks, Objects and Global
Arguments in JavaScript
Tutorial 50
If we would like to make our task repeat every 250 milliseconds for 20 repetitions, we
would write:
tsk.interval = 250;
tsk.repeat(20);
If no arguments are given to the repeat() method, the Task will be scheduled
indefinitely, until we cancel it as follows:
tsk.cancel(); // cancel our task
The execute(), repeat(), and cancel() methods give us all of the flexibility we
need to schedule repeating events in JavaScript. In addition to the ‘interval’ property of
the Task object, we can also find out whether a task is running or not (“running”) and
how many times it has been called (“iterations”), for example.
One important thing to keep in mind is that all methods in the js object
(whether triggered by Max messages or scheduled internally through
tasks) are executed at low priority in the Max scheduler. This means that,
while they will always execute and send data to Max in the correct order,
they cannot be relied on for critically accurate timing if the scheduler is
overloaded with other actions.
Once we’ve defined our Task tsk, we trigger it through the bang() method to our js
object:
function bang()
{
tsk.cancel(); // cancel the bounce, if it's going already
count = 0; // reset the number of bounces
decay = 1.0; // reset the initial decay
tsk.interval = glob.starttime; // set the initial task interval
tsk.repeat(); // start the bouncing
}
When any of our js objects receive a bang, they cancel any previously scheduled tsk tasks,
reset some variables that are relevant to the task function, set an initial timing interval for
the task, and then start it going again.
•
Start a ‘bounce’ in the Tutorial patch by clicking the button at the top. Click the
message box labeled stop. The notes should cease.
321