debounce-fn
Debounce a function
Last updated a month ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install debounce-fn 
SYNC missed versions from official npm registry.

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 is invoked.

This can be used to control the rate of calls handled in a constant stream. For example, a media player may send updates every few milliseconds, but you only want to handle them once a second.

before

Type: boolean
Default: false

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

For example, this 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.

Related

  • debounce - Similar, but simpler. This package adds support for the maxWait and before/after options.
  • p-debounce - Debounce promise-returning & async functions

Current Tags

  • 7.0.0                                ...           latest (a month ago)

14 Versions

  • 7.0.0                                ...           a month ago
  • 6.0.0                                ...           3 years ago
  • 5.1.2                                ...           4 years ago
  • 5.1.1                                ...           4 years ago
  • 5.1.0                                ...           5 years ago
  • 5.0.0                                ...           6 years ago
  • 4.0.0                                ...           7 years ago
  • 3.0.1                                ...           8 years ago
  • 3.0.0                                ...           8 years ago
  • 1.0.0                                ...           9 years ago
  • 0.0.1-security                                ...           11 years ago
  • 2.0.0                                ...           11 years ago
  • 0.1.0                                ...           11 years ago
  • 0.0.0                                ...           12 years ago
Maintainers (1)
Downloads
Total 87
Today 0
This Week 12
This Month 68
Last Day 1
Last Week 27
Last Month 19
Dependencies (1)
Dev Dependencies (3)

© 2010 - cnpmjs.org x YWFE | Home | YWFE