Options
All
  • Public
  • Public/Protected
  • All
Menu

Controls the flow of time.

Type parameters

Hierarchy

Index

Properties

Date

Implements the Date object but using this clock to provide the correct time.

loopLimit

loopLimit: number

Maximum number of timers that will be run when calling runAll().

now

now: number

Current clock time.

performance

performance: { now: any }

Mimics performance.now().

Type declaration

  • now: function
    • now(): number
    • Returns number

timeouts

timeouts: {}

Don't know what this prop is for, but it was included in the clocks that createClock or install return (it is never used in the code, for now).

Type declaration

Methods

cancelAnimationFrame

  • cancelAnimationFrame(id: TTimerId): void
  • Cancel animation frame request.

    Parameters

    • id: TTimerId

      The id returned from requestAnimationFrame method.

    Returns void

cancelIdleCallback

  • cancelIdleCallback(id: TTimerId): void
  • Clears a timer, as long as it was created using requestIdleCallback.

    Parameters

    • id: TTimerId

      Timer ID or object.

    Returns void

clearImmediate

  • clearImmediate(id: TTimerId): void
  • Clears a timer, as long as it was created using setImmediate.

    Parameters

    • id: TTimerId

      Timer ID or object.

    Returns void

clearInterval

  • clearInterval(id: TTimerId): void
  • Clears a timer, as long as it was created using setInterval.

    Parameters

    • id: TTimerId

      Timer ID or object.

    Returns void

clearTimeout

  • clearTimeout(id: TTimerId): void
  • Clears a timer, as long as it was created using setTimeout.

    Parameters

    • id: TTimerId

      Timer ID or object.

    Returns void

countTimers

  • countTimers(): number
  • Get the number of waiting timers.

    Returns number

    number of waiting timers.

next

  • next(): number
  • Advances the clock to the the moment of the first scheduled timer, firing it.

    Returns number

    Fake milliseconds since the unix epoch.

nextAsync

  • Advances the clock to the the moment of the first scheduled timer, firing it.

    Also breaks the event loop, allowing any scheduled promise callbacks to execute before running the timers.

    Returns Promise<number>

    Fake milliseconds since the unix epoch.

requestAnimationFrame

  • requestAnimationFrame(callback: (time: number) => void): TTimerId
  • Schedule callback to run in the next animation frame.

    Parameters

    • callback: (time: number) => void

      Callback to be fired.

        • (time: number): void
        • Parameters

          • time: number

          Returns void

    Returns TTimerId

    Request id.

requestIdleCallback

  • requestIdleCallback(callback: () => void, timeout?: number): TTimerId
  • Queues the callback to be fired during idle periods to perform background and low priority work on the main event loop.

    remarks

    Callbacks which have a timeout option will be fired no later than time in milliseconds.

    Parameters

    • callback: () => void

      Callback to be fired.

        • (): void
        • Returns void

    • Optional timeout: number

      The maximum number of ticks before the callback must be fired.

    Returns TTimerId

reset

  • reset(): void
  • Removes all timers and tick without firing them and restore now to its original value.

    Returns void

runAll

  • runAll(): number
  • Runs all pending timers until there are none remaining.

    remarks

    If new timers are added while it is executing they will be run as well.

    Returns number

    Fake milliseconds since the unix epoch.

runAllAsync

  • Runs all pending timers until there are none remaining.

    Also breaks the event loop, allowing any scheduled promise callbacks to execute before running the timers.

    remarks

    If new timers are added while it is executing they will be run as well.

    Returns Promise<number>

    Fake milliseconds since the unix epoch.

runToFrame

  • runToFrame(): number
  • Advanced the clock to the next animation frame while firing all scheduled callbacks.

    Returns number

    Fake milliseconds since the unix epoch.

runToLast

  • runToLast(): number
  • Takes note of the last scheduled timer when it is run, and advances the clock to that time firing callbacks as necessary.

    Returns number

    Fake milliseconds since the unix epoch.

runToLastAsync

  • runToLastAsync(): Promise<number>
  • Takes note of the last scheduled timer when it is run, and advances the clock to that time firing callbacks as necessary.

    Also breaks the event loop, allowing any scheduled promise callbacks to execute before running the timers.

    Returns Promise<number>

    Fake milliseconds since the unix epoch.

setImmediate

  • setImmediate(callback: (...args: any[]) => void, ...args: any[]): TTimerId
  • Schedules the callback to be fired once 0 milliseconds have ticked by.

    remarks

    You'll still have to call clock.tick() for the callback to fire.

    remarks

    If called during a tick the callback won't fire until 1 millisecond has ticked by.

    Parameters

    • callback: (...args: any[]) => void

      Callback to be fired.

        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Rest ...args: any[]

      Any extra arguments to pass to the callback.

    Returns TTimerId

setInterval

  • setInterval(callback: (...args: any[]) => void, timeout: number, ...args: any[]): TTimerId
  • Schedules a callback to be fired every time timeout milliseconds have ticked by.

    Parameters

    • callback: (...args: any[]) => void

      Callback to be fired.

        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • timeout: number

      How many ticks to wait between callbacks.

    • Rest ...args: any[]

      Any extra arguments to pass to the callback.

    Returns TTimerId

    Time identifier for cancellation.

setSystemTime

  • setSystemTime(now?: number | Date): void
  • Simulates a user changing the system clock.

    remarks

    This affects the current time but it does not in itself cause timers to fire.

    Parameters

    • Optional now: number | Date

      New system time.

    Returns void

setTimeout

  • setTimeout(callback: (...args: any[]) => void, timeout: number, ...args: any[]): TTimerId
  • Schedules a callback to be fired once timeout milliseconds have ticked by.

    Parameters

    • callback: (...args: any[]) => void

      Callback to be fired.

        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • timeout: number

      How many ticks to wait to run the callback.

    • Rest ...args: any[]

      Any extra arguments to pass to the callback.

    Returns TTimerId

    Time identifier for cancellation.

tick

  • tick(time: string | number): number
  • Advance the clock, firing callbacks if necessary.

    Parameters

    • time: string | number

      How many ticks to advance by.

    Returns number

    Fake milliseconds since the unix epoch.

tickAsync

  • tickAsync(time: string | number): Promise<number>
  • Advance the clock, firing callbacks if necessary.

    Also breaks the event loop, allowing any scheduled promise callbacks to execute before running the timers.

    Parameters

    • time: string | number

      How many ticks to advance by.

    Returns Promise<number>

    Fake milliseconds since the unix epoch.