Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Timer

A more accurate timer than setInterval. Requires a callback but works as a class instead of a function, and provides functions for start and stop.

The timer works with setTimer instead of setInterval, safechecking the amount of time that SHOULD have passed since the start of the timer and takes that difference into account for setTimer. That means that you can be sure that with an input of 1000ms, after 10 minutes the callback function has been called 600 times.

example

myTimer = new Timer(() => console.log('One seconds passed!', 1000)); myTimer.stop();

Hierarchy

  • Timer

Index

Constructors

Properties

Methods

Constructors

constructor

  • new Timer(callback: any, duration?: number): Timer
  • Parameters

    • callback: any
    • Default value duration: number = 1000

    Returns Timer

Properties

Private duration

duration: number

Interval duration in ms, defaults to 1000

Private startTime

startTime: number

Starttime of when the timer starts, used for the accuracy calculation

Private timerId

timerId: Timeout

Id returned by setTimer, used to stop the timer

Private totalTicks

totalTicks: number

The amount of ticks/callbacks that has been invoked for the current instance

Methods

reset

  • reset(): void
  • Resets the start time and the ticks of the timer (used for safechecking the timing accuracy), making it ready for another start().

    Returns void

start

  • start(callback: any): void
  • Starts the timer, invoking the callback

    Parameters

    • callback: any

      The callback to invoke

    Returns void

stop

  • stop(): void
  • Stops the timer by clearing the current timerId for the setTimer

    Returns void

Generated using TypeDoc