Classes


Last modified: 2012-12-5

Class Task

A task is a function that can be scheduled or repeated. You can set the arguments to the function as well as the object that will be this when the function is called.

Task in Cycling '74 Reference

Class Summary
Constructor Attributes Constructor Name and Description
 
Task()
A task is a function that can be scheduled or repeated.
Field Summary
Field Attributes Field Name and Description
Array get/set 
The arguments passed to the task function.
Function get/set 
The function that is executed in the Task.
Integer get/set 
The time in milliseconds between repeats of the task function.
Integer get 
The number of times the task function has been called.
Object get/set 
The object that is assigned to be the this in the task function.
Boolean get 
Whether the Task is running or not.
Method Summary
Method Attributes Method Name and Description
void 
cancel()
If a task is scheduled or repeating, any future executions are cancelled.
void 
Run the task once, right now.
void 
repeat(number, initialdelay)
Repeat a task function.
void 
schedule(delay)
Run the task once, with a delay.
Class Detail

Task

()
A task is a function that can be scheduled or repeated. You can set the arguments to the function as well as the object that will be this when the function is called.
var tsk = new Task(function, object, arguments);
The object argument represents the this during the execution of the function. Use the this keyword (referring to the jsthis object) to be able to use outlets and other js object features. The function argument represents the function you want to execute, and arguments (an array) represents the arguments to pass to the function. The object and arguments arguments are optional. If not present, the parent of the function object (typically jsthis) will be assumed, and there will be no arguments supplied to the function.

Although the overall timing accuracy of a Task function is high, the latency between the scheduled time and the actual execution time of a Task function is variable because the function runs in a low-priority thread. Therefore you should avoid using a Task function in a time-critical operation.

For convenience, a Task object is a property of the function executed in a Task. To access the Task from within its function, use the following standard Javascript syntax:
arguments.callee.task
Example:
function ticker(a,b,c)
{
	post("tick");
}

args = new Array(3);
args[0] = 1;
args[1] = 2;
args[2] = 3;
t = new Task(ticker,this,args);
Field Detail
{Array get/set}

arguments

The arguments passed to the task function. arguments[0] is the first argument.
Example:
//TODO

{Function get/set}

function

The function that is executed in the Task. You can even change this within the task function itself.

{Integer get/set}

interval

The time in milliseconds between repeats of the task function. The default interval is 500 ms. Here is an example of a Task with a function that causes the Task to run 10% more slowly each time the function is called, which uses the arguments.callee.task syntax mentioned above:
Example:
function taskfun(){
	var intv = arguments.callee.task.interval;
	arguments.callee.task.interval = intv + (intv * 0.1);
}
Default Value:
500

{Integer get}

iterations

The number of times the task function has been called. Outside of a task function, the value of iterations is always 0. The value resets each time the task is started (using the repeat(), execute(), or schedule() methods described in the next section).
Example:
//TODO

{Object get/set}

object

The object that is assigned to be the this in the task function. Most often this will be your jsthis object, so you can, for example access the outlet() method. You set up your jsthis object to be the this by creating a task with the keyword this as the first argument.

Example:
Example:
 // If the object property of a task is a js object, the following three
 // lines of code are identical from within a task function:

 arguments.callee.task.object.outlet(1,"bang");
 outlet(1,"bang");
 this.outlet(1,"bang");

{Boolean get}

running

Whether the Task is running or not. Within a function executing within a task, this will always be 1.
Example:
//TODO
Method Detail
{void}

cancel

()
If a task is scheduled or repeating, any future executions are cancelled. This method can be used within a task function for a self-canceling Task. The following example is a task function that will run only once, even if it is started using the repeat() function.
Example:
function once() {
	arguments.callee.task.cancel();
}

{void}

execute

()
Run the task once, right now. Equivalent to calling the task function with its arguments.
Example:
//TODO

{void}

repeat

(number, initialdelay)
Repeat a task function. The optional number argument specifies the number of repetitions. If the argument is not present or is negative, the task repeats until it is cancelled. The optional initialdelay argument sets the delay in milliseconds until the first iteration.
Example:
tsk = new Task(repeater_function, this);
tsk.interval = 1000; // every second
tsk.repeat(3);  // do it 3 times

// Here is a repeater function that posts its
// iteration count to the Max window:

function repeater_function()
	post(arguments.callee.task.iterations);
}

// In the above example, the Max window output would be:
// 1 2 3
Parameters:
{Integer} number
{Integer} initialdelay
 

{void}

schedule

(delay)
Run the task once, with a delay. The optional delay argument sets the time (in milliseconds) before the task function will be executed.
Example:
//TODO
Parameters:
{Integer} delay
 

©2012-2013
Max MSP Documentation copied to JsDoc by Tim Schenk SEO Frelancer Berlin. Some examples are by him. Some descriptions of classes, interfaces, properties, methods or events are by him. Until now, most of it is a copy. Published with permission of Cycling '74. This document might be wrong or incomplete. All informations without any warranty. All trademarks are the property of their respective owners. Documentation generated by JsDoc Toolkit on 2012-12-5