Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

useThrottle

import { useCallback, useRef } from 'react'

export default function useThrottle(callback: (...args: unknown[]) => void, delay = 20) {
  const isThrottled = useRef(false)

  const throttledCallback = useCallback(
    (...args) => {
      if (isThrottled.current) return

      callback(args)
      isThrottled.current = true
      setTimeout(() => (isThrottled.current = false), delay)
    },
    [callback, delay]
  )

  return throttledCallback
}
 
PREVIOUS NEXT
Tagged: #useThrottle
ADD COMMENT
Topic
Name
6+2 =