<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet href="_c74_vig.xsl" type="text/xsl"?>
<vignette name="The Task Object" package="Max">
<h1>The Task Object</h1>
<p>
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.
</p>
<h2>Task Constructor</h2>
<code language="javascript">var tsk = new Task(function, object, arguments);
</code>
<p>
The object argument represents the this during the execution of the function. Use the this keyword (referring to the <b>jsthis</b> object) to be able to use outlets and other <o>js</o> 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.
</p>
<p>
<b>Example:</b>
</p>
<code language="javascript">
  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);
</code>
<p>
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.
</p>
<p>
    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:
</p>
<p>
    arguments.callee.task
</p>
<p>
    We'll show you an example of this syntax for a Task that changes its interval below.
</p>
<jsproperty_list name="Task">
    <jsproperty name="arguments" get="1" set="1" type="Array" >
        <description>
            The arguments passed to the task function. arguments[0] is the first argument.
        </description>
    </jsproperty>
    <jsproperty name="function" get="1" set="1" type="Function" >
        <description>
            The function that is executed in the Task. You can even change this within the task function itself.
        </description>
    </jsproperty>
    <jsproperty name="running" get="1" set="0" type="Boolean" >
        <description>
            Whether the Task is running or not. Within a function executing within a task, this will always be 1.
        </description>
    </jsproperty>
    <jsproperty name="interval" get="1" set="1" type="number" default="500" >
        <description>
            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:
            <code language="javascript">
    function taskfun()
    {
      var intv = arguments.callee.task.interval;
      arguments.callee.task.interval = intv + (intv * 0.1);
    }
            </code>
        </description>
    </jsproperty>
    <jsproperty name="object" get="1" set="1" type="Object" >
        <description>
            The object that is assigned to be the this in the task function. Most often this will be your <b>jsthis</b> object, so you can, for example access the outlet() method. You set up your <b>jsthis</b> object to be the this by creating a task with the keyword this as the first argument. Example:

            If the object property of a task is a <o>js</o> object, the following three lines of code are identical from within a task function:

            <code language="javascript">
    arguments.callee.task.object.outlet(1,"bang");
    outlet(1,"bang");
    this.outlet(1,"bang");
            </code>
        </description>
    </jsproperty>
    <jsproperty name="iterations" get="1" set="0" type="Number" >
        <description>
            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).
        </description>
    </jsproperty>
</jsproperty_list>
<jsmethod_list name="Task">
  <jsmethod name="repeat">
    <description>
      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:
      <code language="javascript">
tsk = new Task(repeater_function, this);
tsk.interval = 1000; // every second
tsk.repeat(3);  // do it 3 times
      </code>
      Here is a repeater function that posts its iteration count to the Max window:
      <code language="javascript">
function repeater_function()
{
  post(arguments.callee.task.iterations);
}
      </code>
      In the above example, the Max window output would be:<br/>
      1<br/>
      2<br/>
      3
    </description>
  </jsmethod>
  <jsmethod name="execute">
    <description>
      Run the task once, right now. Equivalent to calling the task function with its arguments.
    </description>
  </jsmethod>
  <jsmethod name="schedule">
    <arglist>
      <arg name="delay" optional="1" type="number" />
    </arglist>
    <description>
      Run the task once, with a delay. The optional delay argument sets the time (in milliseconds) before the task function will be executed.
    </description>
  </jsmethod>
  <jsmethod name="cancel">
    <description>
      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.
      <code language="javascript">
function once()
{
  arguments.callee.task.cancel();
}
      </code>
    </description>
  </jsmethod>
</jsmethod_list>
</vignette>
