Options
All
  • Public
  • Public/Protected
  • All
Menu

The main timer class.

Example:

new Timer('3:00', () => console.log('Time\'s up'));

See Timer.constructor for more uses

Hierarchy

Index

Constructors

constructor

  • new Timer(timeExpression: string, onTimeout?: Function, onInterval?: Function, options?: TimerOptions): Timer
  • new Timer(timeExpression: string, onTimeout?: Function, onInterval?: Function): Timer
  • new Timer(timeExpression: string, onTimeout?: Function, options?: TimerOptions): Timer
  • new Timer(timeExpression: string, options: TimerOptions): Timer
  • Parameters

    • timeExpression: string

      The starting time to countdown from. Methods like Timer.reset uses this value as the initial time.

    • Optional onTimeout: Function

      function to run when the time reaches 0. this is the timer instance, allowing the function to call methods on the timer.

    • Optional onInterval: Function

      function to run on every interval (by default, every 1 second). Useful for updating UI elements.

    • Optional options: TimerOptions

      set various beheivors on the timer. Pass as an object as the 2nd argument.

    Returns Timer

  • Parameters

    • timeExpression: string
    • Optional onTimeout: Function
    • Optional onInterval: Function

    Returns Timer

  • Parameters

    • timeExpression: string
    • Optional onTimeout: Function
    • Optional options: TimerOptions

    Returns Timer

  • Parameters

    Returns Timer

Properties

_initialTime

_initialTime: string

Starting time of the timer. Other methods, such as Timer.reset uses this as its target to reset to.

_intervalId

_intervalId: number | Timeout

the id of the underlying setInterval, which controls the countdown "ticks". It's often unnecessary to use this.

isPaused

isPaused: boolean = false

whether or not the timer is paused.

options

options: TimerOptions = ...

play

play: () => boolean = ...

Type declaration

    • (): boolean
    • Unpauses the timer. If the timer is already unpaused, this has no effect.

      Returns boolean

resume

resume: () => boolean = ...

Type declaration

    • (): boolean
    • Unpauses the timer. If the timer is already unpaused, this has no effect.

      Returns boolean

time

time: Time

Accessors

days

  • get days(): number
  • set days(n: number): void

hours

  • get hours(): number
  • set hours(n: number): void

initialTime

  • get initialTime(): string
  • set initialTime(val: string): void

intervalId

  • get intervalId(): number | Timeout
  • set intervalId(val: number | Timeout): void
  • Returns number | Timeout

  • Parameters

    • val: number | Timeout

    Returns void

milliseconds

  • get milliseconds(): number
  • set milliseconds(val: number): void

mins

  • get mins(): number
  • set mins(n: number): void

minutes

  • get minutes(): number
  • set minutes(n: number): void

ms

  • get ms(): number
  • set ms(n: number): void

seconds

  • get seconds(): number
  • set seconds(n: number): void

secs

  • get secs(): number
  • set secs(n: number): void

years

  • get years(): number
  • set years(n: number): void

Methods

abs

  • abs(set?: boolean): Time

add

  • add(time: TimeExpression, _ignoreSigns?: boolean): void
  • Parameters

    • time: TimeExpression
    • _ignoreSigns: boolean = false

    Returns void

clearTimer

  • clearTimer(): void

equals

  • equals(time: timeObject): boolean

fmtTime

  • fmtTime(fmt?: string): any
  • Formats the current time as a string, depending on the value of fmt.

    Parameters

    • fmt: string = "%Y:%D:%H:%M:%S.%m"

      a template string that will be used to populate with time values.

      fmtTime looks for special characters in the string that start with %, depending on the next value, fmtTime will replace it with the following

      %Y - years %D - days %H - hours %M - minutes %S - seconds %m - milliseconds %n where n is a number, format the following token to be padded with n zeros if all places can't be filled. E.g. a timer with 5 seconds left, the string "%2%S" will render as "05"

    Returns any

getDaysUI

  • getDaysUI(padding: any): string
  • Parameters

    • padding: any

    Returns string

getFullTimeUI

  • getFullTimeUI(includeMilliseconds?: boolean): string
  • Parameters

    • includeMilliseconds: boolean = false

    Returns string

getHoursUI

  • getHoursUI(padding: any): string
  • Parameters

    • padding: any

    Returns string

getMillisecondsUI

  • getMillisecondsUI(padding: any): string
  • Parameters

    • padding: any

    Returns string

getMinutesUI

  • getMinutesUI(padding: any): string
  • Parameters

    • padding: any

    Returns string

getSecondsUI

  • getSecondsUI(padding?: number): string
  • Parameters

    • padding: number = 2

    Returns string

getYearsUI

  • getYearsUI(padding: any): string
  • Parameters

    • padding: any

    Returns string

gt

  • gt(time: string | number | Time): boolean

gte

  • gte(time: string | number | Time): boolean

inMilliseconds

  • inMilliseconds(): number

lt

  • lt(time: string | number | Time): boolean

lte

  • lte(time: timeObject): boolean
  • Parameters

    • time: timeObject

    Returns boolean

pause

  • pause(): boolean
  • Pauses the timer. If the timer is already paused, this has no effect.

    Returns boolean

reset

  • reset(): void
  • Resets the timer back to its initial time. [[Timer.onTimeout]] does not get called.

    Does not change whether the timer is paused or not.

    Returns void

restart

  • restart(): void

set

  • set(timeExpression: TimeExpression): void
  • Parameters

    • timeExpression: TimeExpression

    Returns void

setFromString

  • setFromString(time: string): void
  • deprecated

    use Timer.set instead

    Change the timer to a given string.

    Parameters

    • time: string

      an expression of a time.

    Returns void

setSign

  • setSign(sign: any): Time

subtract

  • subtract(time: string | number | Time, _ignoreSigns?: boolean): void
  • Parameters

    • time: string | number | Time
    • _ignoreSigns: boolean = false

    Returns void

tick

  • tick(force?: boolean): void
  • Manually decrement the timer by its interval (by default 1 second), triggering the [[Timer.onInterval]] if defined, unless paused.

    Parameters

    • force: boolean = false

      do this even if paused (timer will still remain paused)

    Returns void

toString

  • toString(minUnit?: string | void, maxUnit?: string): string
  • Returns time in "YY:DD:hh:ss:mm.mills" format, optionally truncating some units.

    Parameters

    • Optional minUnit: string | void

      Lowest unit of time to show. Units lower than minUnit will not be shown. 'ms' or '' means include every unit.

    • maxUnit: string = 'y'

      Largest unit of time to show. All units of time larger than this are added to the maxUnit. E.g. '01:01:01.000'.toString('', 'm') returns '61:01.000'

    Returns string

    Formatted string representing the time. If maxUnit is greater than minUnit, returns ''

togglePause

  • togglePause(): boolean
  • Flips the timer to paused if unpaused, and to unpaused if paused.

    Returns boolean

unpause

  • unpause(): boolean
  • Unpauses the timer. If the timer is already unpaused, this has no effect.

    Returns boolean

Legend

  • Class
  • Property
  • Method
  • Accessor
  • Inherited constructor
  • Inherited method
  • Inherited accessor
  • Function
  • Interface

Generated using TypeDoc