UNPKG

3.55 kBTypeScriptView Raw
1// Last module patch version validated against: 3.0.1
2
3/**
4 * Returns the current time as defined by performance.now if available, and Date.now if not.
5 * The current time is updated at the start of a frame; it is thus consistent during the frame, and any timers scheduled during the same frame will be synchronized.
6 * If this method is called outside of a frame, such as in response to a user event, the current time is calculated and then fixed until the next frame,
7 * again ensuring consistent timing during event handling.
8 */
9export function now(): number;
10
11export interface Timer {
12 /**
13 * Restart a timer with the specified callback and optional delay and time.
14 * This is equivalent to stopping this timer and creating a new timer with the specified arguments,
15 * although this timer retains the original invocation priority.
16 *
17 * @param callback A callback function to be invoked and passed in the apparent
18 * elapsed time since the timer became active in milliseconds.
19 * @param delay An optional numeric delay in milliseconds (default = 0) relative to time.
20 * @param time An optional time in milliseconds relative to which the delay is calculated (default = now).
21 */
22 restart(callbackFn: (elapsed: number) => void, delay?: number, time?: number): void;
23
24 /**
25 * Stop the timer.
26 */
27 stop(): void;
28}
29
30/**
31 * Schedules and returns a new timer, invoking the specified callback repeatedly until the timer is stopped.
32 * The callback is passed the (apparent) elapsed time since the timer became active.
33 *
34 * @param callback A callback function to be invoked and passed in the apparent
35 * elapsed time since the timer became active in milliseconds.
36 * @param delay An optional numeric delay in milliseconds (default = 0) relative to time.
37 * @param time An optional time in milliseconds relative to which the delay is calculated (default = now).
38 */
39export function timer(callback: (elapsed: number) => void, delay?: number, time?: number): Timer;
40
41/**
42 * Immediately invoke any eligible timer callbacks.
43 */
44export function timerFlush(): void;
45
46/**
47 * Schedules and returns a new timer, invoking the specified callback. The timer is stopped automatically
48 * on its first callback. The callback is passed the (apparent) elapsed time since the timer became active.
49 *
50 * @param callback A callback function to be invoked and passed in the apparent
51 * elapsed time since the timer became active in milliseconds.
52 * @param delay An optional numeric delay in milliseconds (default = 0) relative to time.
53 * @param time An optional time in milliseconds relative to which the delay is calculated (default = now).
54 */
55export function timeout(callback: (elapsed: number) => void, delay?: number, time?: number): Timer;
56
57/**
58 * Schedules and returns a new timer, invoking the specified callback repeatedly every 'delay' milliseconds
59 * until the timer is stopped.
60 * The callback is passed the (apparent) elapsed time since the timer became active.
61 *
62 * @param callback A callback function to be invoked and passed in the apparent
63 * elapsed time since the timer became active in milliseconds.
64 * @param delay An optional numeric delay in milliseconds between repeat invocations of the callback.
65 * If not specified, the interval timer behaves like the regular timer.
66 * @param time An optional time in milliseconds relative to which the initial delay is calculated (default = now).
67 */
68export function interval(callback: (elapsed: number) => void, delay?: number, time?: number): Timer;