Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Timer

A timer which calls callback function on every ticks.

const timer = new Timer(1000); // every 1000ms
timer.addEventListener('tick', ()=> console.log('Hello!');
timer.start();

You can create an once timer.

const timer = new Timer(1000, { once: true });
timer.addEventListener('tick', ()=> console.log('Hello!');
timer.start();

Note: You should call addEventListener before start the timer. Otherwise, the timer may fire tick event without listeners.

Hierarchy

Index

Constructors

constructor

Properties

Protected _clearIntervalFunction

_clearIntervalFunction: Function

Protected _intervalFunction

_intervalFunction: Function

Protected _intervalMs

_intervalMs: number

Protected _listenerMap

_listenerMap: Map<string, Function[]>

Protected _once

_once: boolean

Protected _timerId

_timerId: Timeout | null

Methods

addEventListener

  • addEventListener(type: string, listener: Function): void
  • Add listener to the dispatcher's event named type.

    Parameters

    • type: string
    • listener: Function

    Returns void

dispatchEvent

  • dispatchEvent(type: string, event?: any): void
  • Calls every listeners registered to type event.

    Parameters

    • type: string
    • Optional event: any

    Returns void

removeAllEventListeners

  • removeAllEventListeners(type?: undefined | string): void
  • Remove all listeners. If type is specified, remove all listeners of type.

    Parameters

    • Optional type: undefined | string

    Returns void

removeEventListener

  • removeEventListener(type: string, listener: Function): boolean
  • Remove the listener from the dispatcher.

    Returns true if listener is removed, otherwise returns false.

    Parameters

    • type: string
    • listener: Function

    Returns boolean

start

  • start(): void
  • Starts the timer.

    Returns void

stop

  • stop(): void
  • Stop the timer.

    Returns void

Generated using TypeDoc