debounce-execution v1.0.9
debounce-execution
Debounce will collapse multiple requests (if any) for a named task into one invocation which will execute after the given wait time has passed. By default the latest request for a task will be executed after the given wait time, but it is possible to execute the first request and block any future requests until the given wait time has passed.
Installation
$ npm install debounce-execution --save
Changelog
v1.0.7
- BREAKING CHANGE: new syntax. Removed 'job names' in favor of equality checking.
- Fixed a bug where the same function could be called multiple times.
v1.0.6
- Added optional Flow typings and built with rollup instead. For a typed version, import from
debounce-execution/typed
instead. - Added an optional version with es-module exports instead of umd. For the native version, import from
debounce-execution/native
instead.
Examples
Debounce sports one method: debounce(func: function, waitTime?: number = 0, immediate?: boolean = false): void
.
This allows you to do:
let scrollhandler = () => debounce(onScroll, 200);
container.addEventListener("scroll", scrollHandler);
function onScroll () {
// Any scroll events fired within the last 200ms has been collapsed and only
// the last event made this function execute.
}
If immediate is true, the first request is executed and then any requests within the given waitTime will be blocked:
let touchStartHandler = () => debounce(onTouchStart, 100, true);
container.addEventListener("touchstart", touchStartHandler);
function onTouchStart (e) {
//Only the first touch event within the next 100ms will execute this function.
}
Usage
Simply do npm install debounce-execution --save
;
And then all that's left is to import what you want:
import Debounce from "debounce-execution"; // Transpiled to ES5
// or
import Debounce from "debounce-execution/typed" // With Flow typings and ES-modules
// or
import Debounce from "debounce-execution/native" // With native ES-modules.
The standard version works with amd
, umd
, commonjs
and iife
:
const Debounce = require("debounce-execution");
License
Copyright (c) 2016 Dlmma IVS. Released under the MIT license.