UNPKG

1.36 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.default = (function (func, wait, options) {
4 var timeout, context, args, result;
5 var previous = 0;
6 if (!options)
7 options = {};
8 var later = function () {
9 previous = options.leading === false ? 0 : Date.now();
10 timeout = null;
11 result = func.apply(context, args);
12 if (!timeout)
13 context = args = null;
14 };
15 var throttled = function () {
16 var now = Date.now();
17 if (!previous && options.leading === false)
18 previous = now;
19 var remaining = wait - (now - previous);
20 context = this;
21 args = arguments;
22 if (remaining <= 0 || remaining > wait) {
23 if (timeout) {
24 clearTimeout(timeout);
25 timeout = null;
26 }
27 previous = now;
28 result = func.apply(context, args);
29 if (!timeout)
30 context = args = null;
31 }
32 else if (!timeout && options.trailing !== false) {
33 timeout = setTimeout(later, remaining);
34 }
35 return result;
36 };
37 throttled.cancel = function () {
38 clearTimeout(timeout);
39 previous = 0;
40 timeout = context = args = null;
41 };
42 return throttled;
43});
44//# sourceMappingURL=throttle.js.map
\No newline at end of file