Skip to content

防抖

js
function debounce(func, wait) {
  let timeout

  return function (...args) {
    const context = this

    clearTimeout(timeout)

    timeout = setTimeout(() => {
      func.apply(context, args)
    }, wait)
  }
}

// Example usage:
const handleResize = () => {
  console.log('Window resized')
}

const debouncedResize = debounce(handleResize, 300)

window.addEventListener('resize', debouncedResize)