1 | "use strict";
|
2 | function debounce(func, wait, immediate) {
|
3 | var timeout, result;
|
4 | var debounced = function () {
|
5 | var _this = this;
|
6 | var args = [];
|
7 | for (var _i = 0; _i < arguments.length; _i++) {
|
8 | args[_i] = arguments[_i];
|
9 | }
|
10 | if (timeout)
|
11 | clearTimeout(timeout);
|
12 | if (immediate) {
|
13 | var callNow = !timeout;
|
14 | timeout = setTimeout(function () {
|
15 | timeout = null;
|
16 | }, wait);
|
17 | if (callNow)
|
18 | result = func.apply(this, args);
|
19 | }
|
20 | else {
|
21 | timeout = setTimeout(function () {
|
22 | func.apply(_this, args);
|
23 | }, wait);
|
24 | }
|
25 | return result;
|
26 | };
|
27 | debounced.cancel = function () {
|
28 | clearTimeout(timeout);
|
29 | timeout = null;
|
30 | };
|
31 | return debounced;
|
32 | }
|
33 | function Debounce(time, immediate) {
|
34 | return function (target, key, descriptor) {
|
35 | descriptor.value = debounce(target[key], time, immediate);
|
36 | };
|
37 | }
|
38 | var obj = { debounce: debounce, Debounce: Debounce };
|
39 | module.exports = obj;
|
40 |
|
\ | No newline at end of file |