1 |
|
2 |
|
3 | |
4 |
|
5 |
|
6 | function debounce(fn, threshold, isAsap){
|
7 | var timeout, result;
|
8 | function debounced(){
|
9 | var args = arguments, context = this;
|
10 | function delayed(){
|
11 | if (! isAsap) {
|
12 | result = fn.apply(context, args);
|
13 | }
|
14 | timeout = null;
|
15 | }
|
16 | if (timeout) {
|
17 | clearTimeout(timeout);
|
18 | } else if (isAsap) {
|
19 | result = fn.apply(context, args);
|
20 | }
|
21 | timeout = setTimeout(delayed, threshold);
|
22 | return result;
|
23 | }
|
24 | debounced.cancel = function(){
|
25 | clearTimeout(timeout);
|
26 | };
|
27 | return debounced;
|
28 | }
|
29 |
|
30 | module.exports = debounce;
|
31 |
|
32 |
|