You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.3 KiB
42 lines
1.3 KiB
export default (function (func, wait, options) {
|
|
var timeout, context, args, result;
|
|
var previous = 0;
|
|
if (!options)
|
|
options = {};
|
|
var later = function () {
|
|
previous = options.leading === false ? 0 : Date.now();
|
|
timeout = null;
|
|
result = func.apply(context, args);
|
|
if (!timeout)
|
|
context = args = null;
|
|
};
|
|
var throttled = function () {
|
|
var now = Date.now();
|
|
if (!previous && options.leading === false)
|
|
previous = now;
|
|
var remaining = wait - (now - previous);
|
|
context = this;
|
|
args = arguments;
|
|
if (remaining <= 0 || remaining > wait) {
|
|
if (timeout) {
|
|
clearTimeout(timeout);
|
|
timeout = null;
|
|
}
|
|
previous = now;
|
|
result = func.apply(context, args);
|
|
if (!timeout)
|
|
context = args = null;
|
|
}
|
|
else if (!timeout && options.trailing !== false) {
|
|
timeout = setTimeout(later, remaining);
|
|
}
|
|
return result;
|
|
};
|
|
throttled.cancel = function () {
|
|
clearTimeout(timeout);
|
|
previous = 0;
|
|
timeout = context = args = null;
|
|
};
|
|
return throttled;
|
|
});
|
|
//# sourceMappingURL=throttle.js.map
|