|
|
**Basic syntax:**
|
|
|
# debounce
|
|
|
|
|
|
`debounce(func,[wait=0],[reverse=false])`
|
|
|
## description
|
|
|
Returns a function that will only run N milliseconds after it stops being called. Or optionally will only run once during multiple calls, and won't run again until N milliseconds after the last call.
|
|
|
|
|
|
Work like any standard debounce function. The only thing needs to be notice is that if set the last argument to true. Then it will accept the first function call during the duration instead of the last one like the standard way.
|
|
|
## requires
|
|
|
* nothing
|
|
|
|
|
|
**Usage case:**
|
|
|
## parameters
|
|
|
* func - required - function to be debounced
|
|
|
* wait - required - delay after last call fire the func
|
|
|
* immediate - optional - triggers the func immediately but then won't trigger it again until the wait time has passed after the last call
|
|
|
|
|
|
`document.addEventListener('scroll', debounce(_onScroll, 50));`
|
|
|
## returns
|
|
|
* debounced function
|
|
|
|
|
|
## example usage:
|
|
|
```js
|
|
|
var resized = debounce(function() {
|
|
|
// function to run, 250ms after the resize has ended
|
|
|
}, 250);
|
|
|
|
|
|
window.addEventListener('resize', resized);
|
|
|
```
|
|
|
|
|
|
```js
|
|
|
var clicked = debounce(function() {
|
|
|
// function to run on first click, but won't run again until clicking has stopped for 1000ms
|
|
|
}, 1000, true);
|
|
|
|
|
|
document.addEventListener('click', clicked);
|
|
|
```
|
|
|
|
|
|
## thanks to:
|
|
|
https://davidwalsh.name/javascript-debounce-function |