zent
Version:
一套前端设计语言和基于React的实现
40 lines (39 loc) • 1.05 kB
JavaScript
export default function debounce(func, wait, options) {
if (wait === void 0) { wait = 0; }
if (options === void 0) { options = {}; }
var timeout;
var result;
var later = function (args) {
timeout = null;
if (args !== undefined) {
result = func.apply(void 0, args);
}
};
var debounced = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
if (timeout) {
clearTimeout(timeout);
}
if (options.immediate) {
var callNow = !timeout;
timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(void 0, args);
}
}
else {
timeout = setTimeout(function () {
later(args);
}, wait);
}
return result;
};
debounced.cancel = function () {
clearTimeout(timeout);
timeout = null;
};
return debounced;
}