zarm-web
Version:
基于 React 的桌面端UI库
26 lines (23 loc) • 508 B
JavaScript
export default function debounce(func, wait, immediate) {
let timeout;
let result;
return function debounced(...args) {
if (timeout) {
clearTimeout(timeout);
}
if (immediate) {
const callNow = !timeout;
timeout = setTimeout(() => {
timeout = null;
}, wait);
if (callNow) {
result = func.apply(this, args);
}
} else {
timeout = setTimeout(() => {
func.apply(this, args);
}, wait);
}
return result;
};
}