# Scheduler

## util Scheduler

A scheduler helps run the main loop by managing scheduled functions, called tasks, after each frame is displayed.

Tasks are either another `Scheduler`, or a JavaScript functions returning one of the following codes:

- Scheduler.Event.NEXT: Move onto the next task *without* rendering the scene first.
- Scheduler.Event.FLIP_REPEAT: Render the scene and repeat the task.
- Scheduler.Event.FLIP_NEXT: Render the scene and move onto the next task.
- Scheduler.Event.QUIT: Quit the scheduler.

It is possible to create sub-schedulers, e.g. to handle loops. Sub-schedulers are added to a parent scheduler as a normal task would be by calling `scheduler.add(subScheduler)`.

Conditional branching is also available: `scheduler.addConditionalBranches`

## Constructor

#### new Scheduler(psychoJS)

Source:
  - util/Scheduler.js, line 32

##### Parameters:

Name | Type | Description
--- | --- | ---
`psychoJS` | module:core.PsychoJS | the PsychoJS instance

### Members

#### (static, readonly) Event :Symbol

Source:
  - util/Scheduler.js, line 252

##### Properties:

Name | Type | Description
--- | --- | ---
`NEXT` | Symbol | Move onto the next task without rendering the scene first.
`FLIP_REPEAT` | Symbol | Render the scene and repeat the task.
`FLIP_NEXT` | Symbol | Render the scene and move onto the next task.
`QUIT` | Symbol | Quit the scheduler.

Events.

##### Type:

- Symbol

#### (static, readonly) Status :Symbol

Source:
  - util/Scheduler.js, line 280

##### Properties:

Name | Type | Description
--- | --- | ---
`RUNNING` | Symbol | The Scheduler is running.
`STOPPED` | Symbol | The Scheduler is stopped.

Status.

##### Type:

- Symbol

#### status

Source:
  - util/Scheduler.js, line 58

Get the status of the scheduler.

### Methods

#### add(task, …args)

Source:
  - util/Scheduler.js, line 75

Schedule a new task.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`task` | Scheduler~Task | Scheduler |  | the task to be scheduled
`args` | * | <repeatable> | arguments for that task

#### addConditional(condition, thenScheduler, elseScheduler)

Source:
  - util/Scheduler.js, line 96

Schedule a series of task or another, based on a condition.

Note: the tasks are `sub-schedulers`.

##### Parameters:

Name | Type | Description
--- | --- | ---
`condition` | Scheduler~Condition | the condition
`thenScheduler` | Scheduler | the `Scheduler` to be run if the condition is satisfied
`elseScheduler` | Scheduler | the `Scheduler` to be run if the condition is not satisfied

#### (async) start()

Source:
  - util/Scheduler.js, line 121

Start this scheduler.

Note: tasks are run after each animation frame.

#### stop()

Source:
  - util/Scheduler.js, line 163

Stop this scheduler.

### Type Definitions

#### Condition() → {boolean}

Source:
  - util/Scheduler.js, line 81

Condition evaluated when the task is run.

##### Returns:

Type
  boolean

#### Task(argsopt)

Source:
  - util/Scheduler.js, line 63

Task to be run by the scheduler.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`args` | * | <optional> | optional arguments