1. export function debounce(callback, timeout = 500) {
  2. let timer = null;
  3. return (...args) => {
  4. if(timer !== null) clearTimeout(timer);
  5. timer = setTimeout(() => {
  6. callback.call(this, ...args);
  7. timer = null; // 执行完之后,重置timer
  8. }, timeout);
  9. }
  10. }
  11. debounce(() => {}, 1000)

lodash.debounce

  1. import debounce from 'lodash.debounce'
  2. // debounce(func, wait, options)
  3. const onInput = debounce((e) => {}, 150)
  4. const onInput = debounce((e) => {}, 150, {
  5. loading: false, // 初始化不执行
  6. trailing: true,
  7. maxWait: 1000
  8. })

debounce.png

debounce参数

lodash.debounce.png