Skip to content

Task

Defined in: src/Task.ts:50

Represents a task that holds the configuration and the callback to be run by a TaskTimer on its tick intervals.

A task can be created implicitly via TaskTimer.add (passing options or a callback) or explicitly with the Task constructor when a stable reference is needed up front.

const task = new Task({
id: 'heartbeat',
tickInterval: 5,
totalRuns: 10,
callback(task) {
console.log(`${task.id} ran ${task.currentRuns} times`);
}
});
timer.add(task);

TData = any

new Task<TData>(options): Task<TData>

Defined in: src/Task.ts:80

Creates a new Task.

ITaskOptions<TData>

Task options. A unique id and a callback are required.

Task<TData>

get id(): string

Defined in: src/Task.ts:91

Unique ID of the task.

string


get enabled(): boolean

Defined in: src/Task.ts:99

Whether the task is currently enabled. While false, the task bypasses its callback — a manual on/off switch over execution.

boolean

set enabled(value): void

Defined in: src/Task.ts:102

boolean

void


get tickDelay(): number

Defined in: src/Task.ts:109

Number of ticks to wait before running the task for the first time.

number

set tickDelay(value): void

Defined in: src/Task.ts:112

number

void


get tickInterval(): number

Defined in: src/Task.ts:121

Tick interval the task runs on. The unit is ticks, not milliseconds: with a timer interval of 1000 ms, a tickInterval of 5 runs the task every 5 seconds.

number

set tickInterval(value): void

Defined in: src/Task.ts:124

number

void


get totalRuns(): number | null

Defined in: src/Task.ts:132

Total number of times the task should run. 0 or null means unlimited (until the timer stops).

number | null

set totalRuns(value): void

Defined in: src/Task.ts:135

number | null

void


get defer(): boolean

Defined in: src/Task.ts:144

Whether to defer the callback to the next event-loop turn (via setImmediate) before executing. Useful when the task synchronously blocks the event loop, so it yields rather than running inline on the tick.

boolean

set defer(value): void

Defined in: src/Task.ts:147

boolean

void


get lead(): boolean

Defined in: src/Task.ts:156

Whether the task runs once immediately when the timer starts (the leading edge), in addition to its normal tick schedule. A future startDate still defers the first run. Takes effect on start() only.

boolean

set lead(value): void

Defined in: src/Task.ts:159

boolean

void


get data(): TData

Defined in: src/Task.ts:167

Arbitrary user data attached to the task, available here and in event listeners. Typed via the task’s TData parameter.

TData

set data(value): void

Defined in: src/Task.ts:170

TData

void


get currentRuns(): number

Defined in: src/Task.ts:177

Number of times the task has run.

number


get time(): ITimeInfo

Defined in: src/Task.ts:185

Lifetime information for the task: started, stopped (0 if still running) and elapsed, in milliseconds.

ITimeInfo


get callback(): TaskCallback<TData>

Defined in: src/Task.ts:200

Callback executed on each run.

TaskCallback<TData>


get removeOnCompleted(): boolean

Defined in: src/Task.ts:208

Whether to remove the task (freeing memory) once it has completed. Requires totalRuns and/or stopDate to be set.

boolean

set removeOnCompleted(value): void

Defined in: src/Task.ts:211

boolean

void


get completed(): boolean

Defined in: src/Task.ts:219

Whether the task has completed all of its runs or reached its stopDate. Always false when neither totalRuns nor stopDate is set.

boolean

reset(options?): Task<TData>

Defined in: src/Task.ts:270

Resets the current run count, keeping the task running for the same tickInterval as initially configured. Optionally re-configures the task.

ITaskBaseOptions<TData>

New options to apply. The task id cannot be changed.

Task<TData>

The task instance for chaining.

If options tries to change the task id.


toJSON(): Record<string, any>

Defined in: src/Task.ts:289

Serializes the task to a plain object (excluding the callback). Used by JSON.stringify.

Record<string, any>