signaltimer
TypeScript icon, indicating that this package has built-in type declarations

1.5.0 • Public • Published

signaltimer

A wrapper function for setInterval(), setTimeout() and requestAnimationFrame() that uses AbortSignal() as cancellation strategy

Install

npm install signaltimer

Usage

import { setSignalInterval, setSignalTimeout } from 'signaltimer';

const controller = new AbortController();

setSignalInterval(() => {
  console.log('Hello');
}, controller.signal, 1000);

setSignalTimeout(() => {
  console.log('Hello');
}, controller.signal, 1000);

Available functions

/**
 * A `setInterval()` wrapper with support for `AbortSignal`
 * 
 * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/setInterval)
 */
function setSignalInterval(handler: Function, signal?: AbortSignal, ms?: number | undefined, ...args: any[]): CancelTimerFunction
/**
 * A `setTimeout()` wrapper with support for `AbortSignal`
 * 
 * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout)
 */
function setSignalTimeout(handler: Function, signal?: AbortSignal, ms?: number | undefined, ...args: any[]): CancelTimerFunction
/**
 * A `requestAnimationFrame()` wrapper with support for `AbortSignal`
 * 
 * **Note:** This function will call the handler immediately, and then call it again on the next animation frame.
 * 
 * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame)
 */
function requestSignalAnimationFrame(handler: FrameRequestCallback, signal?: AbortSignal): CancelAnimationFrame
/**
 * @deprecated Use `requestSignalAnimationInterval()` instead
 */
function setAnimationInterval(handler: Function, signal?: AbortSignal, ms?: number | undefined): CancelTimerFunction

/**
 * Similar to `setInterval()` implementation using a combination of `requestAnimationFrame()` and `setTimeout()` with support for `AbortSignal`
 * 
 * **Features**:
 * - Accurate over time
 * - Updates visually steadily
 * - Avoids running in background
 * - Otherwise good CPU usage
 * 
 * [Github Gist](https://gist.github.com/jakearchibald/cb03f15670817001b1157e62a076fe95) | [Youtube](https://www.youtube.com/watch?v=MCi6AZMkxcU)
 */
function requestSignalAnimationInterval(handler: Function, signal?: AbortSignal, ms?: number | undefined): CancelTimerFunction
/**
 * Similar to `requestSignalAnimationInterval` without the use of `requestAnimationFrame()`, can be used in a `SharedWorker`.
 * 
 * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout)
 * 
 * **Further reading**:
 * [Reasons for delays longer than specified](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#reasons_for_delays_longer_than_specified)
 * | [Timeouts in inactive tabs](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#timeouts_in_inactive_tabs)
 * | [Throttling of tracking scripts](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#throttling_of_tracking_scripts)
 * | [Late timeouts](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#late_timeouts)
 */
function setSignalCounterInterval(handler: Function, signal?: AbortSignal, ms?: number | undefined, ...args: any[]): CancelTimerFunction

License

Licensed under the MIT License.

Readme

Keywords

none

Package Sidebar

Install

npm i signaltimer

Weekly Downloads

1

Version

1.5.0

License

MIT

Unpacked Size

32.3 kB

Total Files

7

Last publish

Collaborators

  • andrewalex