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 2 years ago by stephenmathieson .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install debounce 
SYNC missed versions from official npm registry.

debounce

Useful for implementing behavior that should only happen after a repeated action has completed.

Installation

$ component install component/debounce

Or in node:

$ npm install debounce

Example

var debounce = require('debounce');
window.onresize = debounce(resize, 200);

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

To later clear the timer and cancel currently scheduled executions:

window.onresize.clear();

To execute any pending invocations and reset the timer:

window.onresize.flush();

Alternately, if using newer syntax:

import { debounce } from "debounce";

API

debounce(fn, wait, [ immediate || false ])

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.

Pass true for the immediate parameter to cause debounce to trigger the function on the leading edge instead of the trailing edge of the wait interval. Useful in circumstances like preventing accidental double-clicks on a "submit" button from firing a second time.

The debounced function returned has a property 'clear' that is a function that will clear any scheduled future executions of your function.

The debounced function returned has a property 'flush' that is a function that will immediately execute the function if and only if execution is scheduled, and reset the execution timer for subsequent invocations of the debounced function.

License

MIT

Original implementation is from underscore.js which also has an MIT license.

Current Tags

  • 1.2.0                                ...           latest (2 years ago)

6 Versions

  • 1.2.0                                ...           2 years ago
  • 1.1.0                                ...           3 years ago
  • 1.0.2                                ...           3 years ago
  • 1.0.0                                ...           6 years ago
  • 0.0.3                                ...           7 years ago
  • 0.0.2                                ...           7 years ago
Downloads
Today 62
This Week 2,319
This Month 9,955
Last Day 2,257
Last Week 12,742
Last Month 57,374
Dependencies (0)
None
Dev Dependencies (4)
Dependents (1150)

Copyright 2014 - 2017 © taobao.org |