1 | "use strict";
|
2 | function throttle(func, wait, options) {
|
3 | var timeout;
|
4 | var previous = 0;
|
5 | if (!options)
|
6 | options = {};
|
7 | var throttled = function () {
|
8 | var _this = this;
|
9 | var args = [];
|
10 | for (var _i = 0; _i < arguments.length; _i++) {
|
11 | args[_i] = arguments[_i];
|
12 | }
|
13 | var now = new Date().getTime();
|
14 | if (!previous && options.leading === false)
|
15 | previous = now;
|
16 | var remaining = wait - (now - previous);
|
17 | if (remaining <= 0 || now < previous) {
|
18 | if (timeout) {
|
19 | clearTimeout(timeout);
|
20 | timeout = null;
|
21 | }
|
22 | previous = now;
|
23 | func.apply(this, args);
|
24 | }
|
25 | else if (!timeout && options.trailing !== false) {
|
26 | timeout = setTimeout(function () {
|
27 | previous = options.leading === false ? 0 : new Date().getTime();
|
28 | timeout = null;
|
29 | func.apply(_this, args);
|
30 | }, remaining);
|
31 | }
|
32 | };
|
33 | throttled.cancel = function () {
|
34 | var args = [];
|
35 | for (var _i = 0; _i < arguments.length; _i++) {
|
36 | args[_i] = arguments[_i];
|
37 | }
|
38 | clearTimeout(timeout);
|
39 | previous = 0;
|
40 | timeout = null;
|
41 | };
|
42 | return throttled;
|
43 | }
|
44 | module.exports = throttle;
|
45 |
|
\ | No newline at end of file |