This package provides a set of timer-related methods, enhancing `setTimeout` and `setInterval` with support for `AbortSignal`.
npm install signal-timers!NPM Type Definitions
!NPM Version
!npm package minimized gzipped size




A TypeScript library that provides enhanced timer functions with AbortSignal support, allowing for more flexible asynchronous operations.
This open-source library offers signal-aware control for timing functions. It allows you to execute callbacks at regular intervals, delay their execution, or create promises that resolve after a set time. All actions can be interrupted by an AbortSignal, providing flexible and responsive time management in your code.
Install the library using npm or yarn:``bash
npm install signal-timers
yarn add signal-timers
``
You can import the functions individually or all at once:
`typescript``
import { interval, timeout, delay, animationFrame } from 'signal-timers';
Create an interval timer that can be aborted with an AbortSignal.
`typescript
const callback = () => {
console.log("Interval callback");
};
const controller = new AbortController();
interval(callback, 1000, { signal: controller.signal });
// To abort the interval
controller.abort();
`
Set a timeout timer that can be aborted with an AbortSignal.
`typescript
const controller = new AbortController();
timeout(
() => {
console.log("Timeout callback");
},
5000,
{ signal: controller.signal }
);
// To abort the timeout
controller.abort();
`
Create a promise-based delay that can be aborted with an AbortSignal.
`typescript
await delay(5000, { signal: AbortSignal.timeout(1000) });
// This line of code will not execute; the preceding line will be rejected after 1000ms, throwing an AbortError.
`
Set a animation frame callback that can be aborted with an AbortSignal.
`typescript
const controller = new AbortController();
animationFrame(
() => {
console.log("animation callback");
},
{ signal: controller.signal }
);
// To abort the timeout
controller.abort();
`
Set a microtask callback that can be aborted with an AbortSignal.
`typescript
const controller = new AbortController();
microtask(
() => {
console.log("microtask callback");
},
{ signal: controller.signal }
);
// or using Promise style
await delayToNextMicrotask({ signal: controller.signal });
// To abort the microtask
controller.abort();
``
- callback: The callback function to execute.
- ms: The interval time in milliseconds.
- options.signal: An optional AbortSignal to abort the interval.
- callback: The callback function to execute after the timeout.
- ms: The timeout duration in milliseconds.
- options.signal: An optional AbortSignal to abort the timeout.
- callback: The callback function to execute in next animation frame.
- options.signal: An optional AbortSignal to abort the animation frame callback.
- ms: The delay time in milliseconds.
- options.signal: An optional AbortSignal to abort the delay.
- options.signal: An optional AbortSignal to abort the delay.
- callback: The callback function to execute.
- options.signal: An optional AbortSignal to abort the microtask.
This project is licensed under the MIT License - see the LICENSE file for details.