-
Notifications
You must be signed in to change notification settings - Fork 0
Reference
- #registerAllEvents
- #registerEvent
- #timelineInsert
- #unregisterEvent
- addDuration
- addEventListener
- addRunningTime
- checkEvents
- destroyAllSpecialEvents
- destroySpecialEvent
- dispatchEvent
- dispatchSpecialEvent
- formatTime
- removeEventListener
- reset
- setDuration
- setRunningTime
- start
- stop
- timerEndRoutine
- toMs
• new Timer(duration
)
Create a new Timer with a duration
Name | Type | Description |
---|---|---|
duration |
number |
The duration of the timer, in ms |
• Private
#currentTimeout: number
= -1
• Private
#specialEvents: _specialEvents
• _timeline: [number
, number
][] = []
• beginTime: number
= null
• callbacks: event
[] = []
• duration: number
• pauseAmount: number
= 0
• pauseBegin: number
= null
• paused: boolean
= true
• get
ended(): boolean
boolean
• set
onEnd(callback
): void
Add a callback to the onReset event, you may add multiple ones
Name | Type |
---|---|
callback |
Function |
void
• set
onReset(callback
): void
Add a callback to the onReset event, you may add multiple ones
Name | Type |
---|---|
callback |
Function |
void
• set
onStart(callback
): void
Add a callback to the onStart event, you may add multiple ones
Name | Type |
---|---|
callback |
Function |
void
• set
onStop(callback
): void
Add a callback to the onStop event, you may add multiple ones
Name | Type |
---|---|
callback |
Function |
void
• get
runningTime(): number
Gives the current running time of the timer
number
• get
timeLeft(): number
Gives the current time left of the timer
number
▸ Private
#registerAllEvents(): void
void
▸ Private
#registerEvent(event
): boolean
Name | Type |
---|---|
event |
number | number [] |
boolean
▸ Private
#timelineInsert(date
, name
): void
Name | Type |
---|---|
date |
number |
name |
number |
void
▸ Private
#unregisterEvent(event
): boolean
Name | Type |
---|---|
event |
number | number [] |
boolean
▸ addDuration(duration
): void
Add a duration to the timer duration, or removes it if the duration is negative
Name | Type | Description |
---|---|---|
duration |
number |
The duration to add to the timer, in ms |
void
▸ addEventListener(event
, callback
): void
Add an event listener, to trigger at a specific interval of time
Name | Type | Description |
---|---|---|
event |
number |
The interval of time the event should be triggered at |
callback |
Function |
The callback to trigger |
void
▸ addRunningTime(duration
): void
Add a duration to the running time of the timer, or removes it if the provided duration is negative
Name | Type | Description |
---|---|---|
duration |
number |
The duration to add to the running time of the timer, in ms |
void
▸ checkEvents(): void
void
▸ destroyAllSpecialEvents(): void
Destroys all the callbacks of all the special events
void
▸ destroySpecialEvent(event
): void
Destroys all the callbacks of a special event
Name | Type | Description |
---|---|---|
event |
"stop" | "end" | "start" | "reset"
|
The event type to destroy |
void
▸ dispatchEvent(event
): number
Triggers all the callbacks of a specific time interval
Name | Type | Description |
---|---|---|
event |
number |
The interval of the callbacks to trigger |
number
The amount of callbacks triggered
▸ dispatchSpecialEvent(event
): void
Dispatches one of the special events
Name | Type | Description |
---|---|---|
event |
"stop" | "end" | "start" | "reset"
|
The event to dispatch |
void
▸ formatTime(time
, format?
): string
Function to help you format time
Name | Type | Description |
---|---|---|
time |
number |
A time in ms |
format? |
string |
The format, accepted formats are: 'ms', 'ss', 'mm', 'hh', 'dd', 'ww', 'mo', 'yy', 'yyyy', and you may use multiple formats at once, for example: 'hh:mm:ss' |
string
The formatted time
▸ removeEventListener(event
): void
Removes all the callbacks of a specific time interval
Name | Type | Description |
---|---|---|
event |
number |
The event to remove |
void
▸ reset(): void
Resets the timer to its initial state but keeps the events. If you want to reset completely, just create a new timer. Trigger the onReset event.
void
▸ setDuration(duration
): void
Set the duration of the timer
Name | Type | Description |
---|---|---|
duration |
number |
The duration of the timer, in ms |
void
▸ setRunningTime(duration
): void
Sets the running time of the timer(overrides the current running time)
Name | Type | Description |
---|---|---|
duration |
number |
The new running time of the timer |
void
▸ start(): boolean
Starts the timer. Triggers the onStart event.
boolean
Returns true if the timer was paused, false if not
▸ stop(): boolean
Stops/pauses the timer. Triggers the onStop event.
boolean
Returns true if the timer was running, false if not
▸ timerEndRoutine(): void
void
▸ toMs(time
): number
Ensures that the time is in ms, and if not, converts it to ms
Throws
If the time string is not in the correct format
Name | Type | Description |
---|---|---|
time |
string | number
|
The time to convert, if string, it must be in the format: 'number + unit', ex: '1s', '1m', '1h', '1d', '1w', '1mo', '1y' |
number
The time converted to ms