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.
Example
Section titled “Example”const task = new Task({ id: 'heartbeat', tickInterval: 5, totalRuns: 10, callback(task) { console.log(`${task.id} ran ${task.currentRuns} times`); } }); timer.add(task);Type Parameters
Section titled “Type Parameters”TData = any
Constructor
Section titled “Constructor”new Task<
TData>(options):Task<TData>
Defined in: src/Task.ts:80
Creates a new Task.
Parameters
Section titled “Parameters”options
Section titled “options”ITaskOptions<TData>
Task options. A unique id and a callback are required.
Returns
Section titled “Returns”Task<TData>
Accessors
Section titled “Accessors”Get Signature
Section titled “Get Signature”get id():
string
Defined in: src/Task.ts:91
Unique ID of the task.
Returns
Section titled “Returns”string
enabled
Section titled “enabled”Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”boolean
Set Signature
Section titled “Set Signature”set enabled(
value):void
Defined in: src/Task.ts:102
Parameters
Section titled “Parameters”boolean
Returns
Section titled “Returns”void
tickDelay
Section titled “tickDelay”Get Signature
Section titled “Get Signature”get tickDelay():
number
Defined in: src/Task.ts:109
Number of ticks to wait before running the task for the first time.
Returns
Section titled “Returns”number
Set Signature
Section titled “Set Signature”set tickDelay(
value):void
Defined in: src/Task.ts:112
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”void
tickInterval
Section titled “tickInterval”Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”number
Set Signature
Section titled “Set Signature”set tickInterval(
value):void
Defined in: src/Task.ts:124
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”void
totalRuns
Section titled “totalRuns”Get Signature
Section titled “Get Signature”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).
Returns
Section titled “Returns”number | null
Set Signature
Section titled “Set Signature”set totalRuns(
value):void
Defined in: src/Task.ts:135
Parameters
Section titled “Parameters”number | null
Returns
Section titled “Returns”void
Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”boolean
Set Signature
Section titled “Set Signature”set defer(
value):void
Defined in: src/Task.ts:147
Parameters
Section titled “Parameters”boolean
Returns
Section titled “Returns”void
Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”boolean
Set Signature
Section titled “Set Signature”set lead(
value):void
Defined in: src/Task.ts:159
Parameters
Section titled “Parameters”boolean
Returns
Section titled “Returns”void
Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”TData
Set Signature
Section titled “Set Signature”set data(
value):void
Defined in: src/Task.ts:170
Parameters
Section titled “Parameters”TData
Returns
Section titled “Returns”void
currentRuns
Section titled “currentRuns”Get Signature
Section titled “Get Signature”get currentRuns():
number
Defined in: src/Task.ts:177
Number of times the task has run.
Returns
Section titled “Returns”number
Get Signature
Section titled “Get Signature”get time():
ITimeInfo
Defined in: src/Task.ts:185
Lifetime information for the task: started, stopped (0 if still
running) and elapsed, in milliseconds.
Returns
Section titled “Returns”callback
Section titled “callback”Get Signature
Section titled “Get Signature”get callback():
TaskCallback<TData>
Defined in: src/Task.ts:200
Callback executed on each run.
Returns
Section titled “Returns”TaskCallback<TData>
removeOnCompleted
Section titled “removeOnCompleted”Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”boolean
Set Signature
Section titled “Set Signature”set removeOnCompleted(
value):void
Defined in: src/Task.ts:211
Parameters
Section titled “Parameters”boolean
Returns
Section titled “Returns”void
completed
Section titled “completed”Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”boolean
Methods
Section titled “Methods”reset()
Section titled “reset()”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.
Parameters
Section titled “Parameters”options?
Section titled “options?”ITaskBaseOptions<TData>
New options to apply. The task id cannot be changed.
Returns
Section titled “Returns”Task<TData>
The task instance for chaining.
Throws
Section titled “Throws”If options tries to change the task id.
toJSON()
Section titled “toJSON()”toJSON():
Record<string,any>
Defined in: src/Task.ts:289
Serializes the task to a plain object (excluding the callback). Used by
JSON.stringify.
Returns
Section titled “Returns”Record<string, any>