debounce
Creates and returns a new debounced version of the passed function that will postpone its execution until after wait milliseconds have elapsed since the last time it was invoked
Last updated 8 years ago by tootallnate .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install debounce 
SYNC missed versions from official npm registry.

debounce

Delay function calls until a set time elapses after the last invocation

Install

npm install debounce

Usage

import debounce from 'debounce';

function resize() {
	console.log('height', window.innerHeight);
	console.log('width', window.innerWidth);
}

window.onresize = debounce(resize, 200);

(You can also use const debounce = require('debounce'))

To later clear the timer and cancel currently scheduled executions:

window.onresize.clear();

To execute immediately only if you have scheduled invocations and reset the timer:

window.onresize.flush();

To execute immediately and reset the timer if it was previously set:

window.onresize.trigger();

API

debounce(fn, wait, options?)

Creates a debounced function that delays execution until wait milliseconds have passed since its last invocation.

Set the immediate option to true to execute the function immediately at the start of the wait interval, preventing issues such as double-clicks on a button.

The returned function has the following methods:

  • .clear() cancels any scheduled executions.
  • .flush() if an execution is scheduled then it will be immediately executed and the timer will be cleared.
  • .trigger() executes the function immediately and clears the timer if it was previously set.

Related

  • p-debounce - Similar but handles promises
  • throttleit - Throttle a function to limit its execution rate

Current Tags

  • 2.1.1                                ...           latest (2 months ago)

10 Versions

  • 2.1.1                                ...           2 months ago
  • 2.1.0                                ...           6 months ago
  • 2.0.0                                ...           a year ago
  • 1.2.1                                ...           4 years ago
  • 1.2.0                                ...           6 years ago
  • 1.1.0                                ...           7 years ago
  • 1.0.2                                ...           8 years ago
  • 1.0.0                                ...           10 years ago
  • 0.0.3                                ...           11 years ago
  • 0.0.2                                ...           11 years ago

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