Timer Lifecycle
A timer is always in one of four states, exposed by timer.state and enumerated by the State enum, a named export. The control methods move it between them.
States
Section titled “States”| State | Value | Meaning |
|---|---|---|
IDLE | idle | Initial state, and the state after reset(). No tasks have run. |
RUNNING | running | Ticking; tasks are firing. |
PAUSED | paused | Held; tasks and counters are frozen, ready to resume. |
STOPPED | stopped | Halted; tasks and counters are retained but not running. |
import { TaskTimer, State } from 'tasktimer';
timer.state === State.RUNNING;Transitions
Section titled “Transitions”timer.start(); // → RUNNING (resets tick count; keeps tasks)timer.pause(); // → PAUSED (only from RUNNING)timer.resume(); // → RUNNING (from PAUSED; starts the timer if IDLE)timer.stop(); // → STOPPED (only from RUNNING)timer.reset(); // → IDLE (clears ticks and removes all tasks silently)Each method returns the timer, so calls chain: timer.add(task).start().
The transitions are guarded — calling a method from the wrong state is a safe no-op rather than an error:
pause()andstop()do nothing unless the timer isRUNNING.resume()does nothing unless the timer isPAUSED— except fromIDLE, where it acts asstart().start()fromRUNNINGrestarts: it resets the tick count and counters but keeps existing tasks.
Start vs Resume
Section titled “Start vs Resume”start() begins a fresh run — tickCount, taskRunCount, and the timer’s time reset to zero. resume() continues a paused run where it left off, preserving counters and each task’s progress. runCount increments on every start and resume.
timer.start(); // run 1timer.pause();timer.resume(); // still run 1, continuedtimer.runCount; // 2 (one start + one resume)Stop vs Reset
Section titled “Stop vs Reset”stop() halts the timer but keeps everything — tasks, tick count, run counters — so you can inspect or restart. reset() returns the timer to IDLE, clearing the tick count and removing all tasks silently (no taskRemoved events).
timer.stop(); // STOPPED — tasks and counts retainedtimer.reset(); // IDLE — tasks removed, counts clearedBoth pausing and the lifecycle around completion are covered in Pause, Resume & Cleanup. Every transition also emits an event.