@martinstark/throttle-ts
TypeScript icon, indicating that this package has built-in type declarations

1.3.0 • Public • Published

throttle-ts

Correctly typed, generic, tiny (431B), typescript throttle function.

throttle typescript

Yields the return value of the throttled function, or undefined when throttled/cancelled.

The throttled function keeps the type signature of the original function, plus void.

Returns a cancel function which enables cleanup of the timeout, and blocks future calls to the throttled function. Useful when unmounting react/view components.

Usage

import { throttle } from "@martinstark/throttle-ts";
const fn = () => "executed";

const [throttledFn] = throttle(fn, 200);

throttledFn(); // "executed"
throttledFn(); // undefined
throttledFn(); // undefined
setTimeout(throttledFn, 500); // "executed"

Using Cancel

const fn = () => "executed";

const [throttledFn, cancel] = throttle(fn, 200);

throttledFn(); // "executed"
setTimeout(throttledFn, 500); // undefined
cancel();

Readme

Keywords

Package Sidebar

Install

npm i @martinstark/throttle-ts

Weekly Downloads

8

Version

1.3.0

License

MIT

Unpacked Size

6.49 kB

Total Files

6

Last publish

Collaborators

  • martinstark