https://github.com/loadingio/debounce.js
Promise-based function debouncing for JavaScript
https://github.com/loadingio/debounce.js
Last synced: 17 days ago
JSON representation
Promise-based function debouncing for JavaScript
- Host: GitHub
- URL: https://github.com/loadingio/debounce.js
- Owner: loadingio
- Created: 2019-03-19T04:10:47.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-04-30T16:53:55.000Z (almost 3 years ago)
- Last Synced: 2025-11-02T03:14:18.387Z (3 months ago)
- Language: LiveScript
- Homepage:
- Size: 196 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Debounce.js
Debounce.js helps you to debounce your function.
## Features
* Simply by wrapping function in debounce().
* Preserve scope for object methods.
* Support Promise
## Usage
* simply debounce:
````
var myFunc = debounce(function( ... ) {
/* do whatever you want */
});
myFunc(...);
````
* custom delay:
````
var myFunc = debounce(function(p1, p2, ...) { ... }, 123); /* default delay is 500 */
myFunc(...);
````
* sometimes it's convenient to have delay come first:
````
var myFunc = debounce(123, function(p1, p2, ...) { ... });
myFunc(...);
````
* simply delay for a few milliseconds (e.g., 300 ms) is also userful when working with promise:
```
debounce(300).then(function() { ... });
```
* use promise after function is executed:
````
var myFunc = debounce(function( ... ) { ... });
myFunc(...).then(function(ret) {
if(ret) { ... } /* ret is thre return value of the function inside debounce */
});
````
* used with object members:
````
var obj = {
member: debounce(function( ... ) { this.value = 2; },
value: 1
};
````
* clear previously scheduled function call:
````
myFunc();
myFunc.clear();
````
* bypassing debounce and call immediately:
```
myFunc( ... ).now();
```
* overwrite previous set delay value:
```
myFunc.delay(300)();
```
## Reference
- usage:
- constructor
- deb = debounce(f, delay)
- deb = debounce(delay, f)
- debounce(delay).then ...
- methods
- deb.clear - clear all pending calls.
- deb() - call `f` ( pending for delay milliseconds ).
- deb().cancel - cancel pending call.
- deb().now - call `f` immediately, discard previous call.
- deb().then - like promise pattern, execute function in then after `f` finished.
## Compatibility
* debounce.js uses Promise, which is not supported in some browsers like IE. Remember to install polyfill before using debounce.js.
## LICENSE
MIT