All files / throttle throttle.ts

100% Statements 7/7
100% Branches 4/4
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27                                1x 1x 1x 3x 3x 1x 1x        
/**
 * @description 函数节流。在wait秒内最多只执行一次func,且执行第一次触发的func
 * @function throttle
 * @param {Function} func - 要节流的函数
 * @param {number} [wait=300] - 需要节流的毫秒
 * @returns {Function} 返回节流函数
 * @author liukun <919590347@qq.com>
 * @example
 * import { throttle } from '@wont/utils'
 * const testFn = () => {
 *     console.log('throttle running...')
 * }
 * window.addEventListener('mousemove', throttle(testFn, 500))
 */
 
type Func = (...rest: any[]) => void
export function throttle(func: Function, wait = 300): Func {
  let lastTime = 0
  return function next(this: Func, ...rest) {
    const nowTime = Date.now()
    if (nowTime - lastTime >= wait || !lastTime) {
      func.apply(this, rest)
      lastTime = nowTime
    }
  }
}