throttle-debounce-ts
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

throttle-debounce-ts · GitHub license

Throttle and debounce functions written in Typescript.

Features:

  • No thirdparty dependecies
  • Easy to use
  • ES Modules and CommonJS format

Install

npm i throttle-debounce-ts

Usage

Throttle

import { throttle } from "throttle-debounce-ts";
 
const throttleFunc = throttle(1000, () => {
  console.log("Hello Throttle!");
});

Debounce

import { debounce } from "throttle-debounce-ts";
 
const debounceFunc = debounce(1000, () => {
    console.log("Hello Debounce!");
});

Cancel

const throttleFunc = throttle(1000, () => {
    // ...
});
// will cancel throttleFunc
throttleFunc.cancel();
 
const debounceFunc = debounce(1000, () => {
    // ...
});
// will cancel delay of debounceFunc
// callback will exec immediately when you call debounceFunc next time
debounceFunc.cancel();

API

throttle(options, callback)

Returns: Function

Throttle execution of a function.

options

Type: Number

A zero-or-greater delay in milliseconds.

Type: Object

options.delay
A zero-or-greater delay in milliseconds.

options.leading
Optional, defaults to false. If it's true, the function will exec on the first call.

options.trailing
Optional, defaults to false. If it's true, the function will exec after last call.

callback

Type: Function

A function to be executed after delay milliseconds.


debounce(options, callback)

Returns: Function

Debounce execution of a function.

options

Type: Number

A zero-or-greater delay in milliseconds.

Type: Object

options.delay
A zero-or-greater delay in milliseconds.

options.leading
Optional, defaults to false. If it's true, the function will exec on the first call.

callback

Type: Function

A function to be executed after delay milliseconds.

Package Sidebar

Install

npm i throttle-debounce-ts

Weekly Downloads

397

Version

1.1.1

License

MIT

Unpacked Size

15 kB

Total Files

10

Last publish

Collaborators

  • shroxd