debounce-fn

Debounce a function

MIT 13 个版本
安装
npm install debounce-fn
yarn add debounce-fn
pnpm add debounce-fn
bun add debounce-fn
README

debounce-fn

Debounce a function

Install

npm install debounce-fn

Usage

import debounceFunction from 'debounce-fn';

window.onresize = debounceFunction(() => {
	// Do something on window resize
}, {wait: 100});

API

debounceFunction(input, options?)

Returns a debounced function that delays calling the input function until after wait milliseconds have elapsed since the last time the debounced function was called.

It comes with a .cancel() method to cancel any scheduled input function calls.

input

Type: Function

Function to debounce.

options

Type: object

wait

Type: number
Default: 0

Time in milliseconds to wait until the input function is called.

maxWait

Type: number
Default: Infinity

The maximum time the input function is allowed to be delayed before it's invoked.

This can be used to limit the number of calls handled in a constant stream. For example, a media player sending updates every few milliseconds but wants to be handled only once a second.

before

Type: boolean
Default: false

Trigger the function on the leading edge of the wait interval.

For example, can be useful for preventing accidental double-clicks on a "submit" button from firing a second time.

after

Type: boolean
Default: true

Trigger the function on the trailing edge of the wait interval.

  • p-debounce - Debounce promise-returning & async functions
版本列表
6.0.0 2023-11-05
5.1.2 2022-06-11
5.1.1 2022-06-11
5.1.0 2022-01-01
5.0.0 2021-02-28
4.0.0 2020-02-19
3.0.1 2019-04-21
3.0.0 2019-04-10
2.0.0 2016-03-22
1.0.0 2017-06-03
0.1.0 2015-10-10
0.0.1-security 2016-03-23
0.0.0 2014-08-04