333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 
# 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
 
```js
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:
 
```js
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`](http://underscorejs.org/)
  which also has an MIT license.