NuclearDispersionSystem/ant-design-vue-jeecg/node_modules/@antv/util/lib/throttle.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-09-14 14:47:11 +08:00
function throttle(func, wait, options) {
var timeout = void 0,
context = void 0,
args = void 0,
result = void 0;
var previous = 0;
if (!options) options = {};
var later = function later() {
previous = options.leading === false ? 0 : Date.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
var throttled = function throttled() {
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;
}
module.exports = throttle;