# 节流Throttle

设置一个时间,在这段时间内只会执行一次函数。

function throttle(func, wait, options) {
  let leading = true;
  let trailing = true;

  if (typeof func !== 'function') {
    throw new TypeError('Expected a function');
  }
  if (isObject(options)) {
    leading = 'leading' in options ? !!options.leading : leading;
    trailing = 'trailing' in options ? !!options.trailing : trailing;
  }
  return debounce(func, wait, {
    leading,
    trailing,
    'maxWait': wait
  })
}

调用防抖功能,将maxWaitwait设置成一样,即是节流的效果。有点意思。

最近更新时间: 2020/9/6 11:30:38