Class zebkit.util.TasksSet <zebkit.util>

Task set is light-weight class to host number of callbacks methods that are called within a context of one JS interval method execution. The class manages special tasks queue to run it one by one as soon as a dedicated interval for the given task is elapsed

   var tasks = new zebkit.util.TasksSet();

   tasks.run(function(t) {
       // task1 body
       ...
       if (condition) {
           t.shutdown();
       }
   }, 1000, 200);

   tasks.run(function(t) {
       // task2 body
       ...
       if (condition) {
           t.shutdown();
       }
   }, 2000, 300);
Constructor:
zebkit.util.TasksSet ([maxTasks])

Parameters:
  • [maxTasks] <Integer>

    maximal possible number of active tasks in queue.




public <zebkit.util.Task> run (f, [si], [ri])
public void runOnce (f, [startIn])
protected <Boolean> shutdown (t)
public void shutdownAll ( )

public <zebkit.util.Task> run (f, [si], [ri])

Take a free task from pool and run it with the specified body and the given context.


Parameters:
  • f <Function | Object>

    a task function that has to be executed. The task method gets the task context as its argument. You can pass an object as the argument if the object has "run" method implemented. In this cases "run" method will be used as the task body.

  • [si] <Integer>

    time in milliseconds the task has to be executed

  • [ri] <Integer>

    the time in milliseconds the task has to be periodically repeated


Returns: <zebkit.util.Task>

an allocated task


Example:
   var tasks = new zebkit.util.TasksSet();

   // execute task
   var task = tasks.run(function (t) {
       // do something
       ...
       // complete task if necessary
       t.shutdown();
   }, 100, 300);

   // pause task
   task.pause(1000, 2000);

   ...
   // resume task in a second
   task.resume(1000);
   var tasks = new zebkit.util.TasksSet();

   var a = new zebkit.Dummy([
       function run() {
           // task body
           ...
       }
   ]);

   // execute task
   var task = tasks.runOnce(a);
public void runOnce (f, [startIn])

Take a free task from tasks pool and run it once in the specified period of time.


Parameters:
  • f <Function | Object>

    a task function that has to be executed. The task method gets the task context as its argument. You can pass an object as the argument if the object has "run" method implemented. In this cases "run" method will be used as the task body.

  • [startIn] <Integer>

    time in milliseconds the task has to be executed in

protected <Boolean> shutdown (t)

Shutdown the given task


Parameters:
Returns: <Boolean>

true if the task has been stopped, false if the task has not been started to be stopped

public void shutdownAll ( )

Shut down all active at the given moment tasks body and the given context.


private <Number> count

Number of run in the set tasks

private <Number> pid

pid of executed JS interval method callback

private <Number> quantum

Interval