export function debounce(callback, timeout = 500) {
let timer = null;
return (...args) => {
if(timer !== null) clearTimeout(timer);
timer = setTimeout(() => {
callback.call(this, ...args);
timer = null; // 执行完之后,重置timer
}, timeout);
}
}
debounce(() => {}, 1000)
lodash.debounce
import debounce from 'lodash.debounce'
// debounce(func, wait, options)
const onInput = debounce((e) => {}, 150)
const onInput = debounce((e) => {}, 150, {
loading: false, // 初始化不执行
trailing: true,
maxWait: 1000
})