debounce


The debounce function is a utility that creates a new function, that when invoked, delays the execution of the callback function until after a certain number of milliseconds (wait) have passed since the last time it was invoked.

Parameters

  • callback: (...args: any[]) => void - The function to be executed after the wait time has passed since its last invocation.
  • wait: number - The number of milliseconds to delay the execution of the callback function.

Returns

  • (...args: any[]) => void - A new function that delays the execution of the callback function.

Usage

import { debounce } from 'reactchemy'

export default function Component() {
const [two] = debounce()

return <div>Hello {two}</div>
}

Util

export function debounce(callback: (...args: any[]) => void, wait: number): (...args: any[]) => void {
   let timeoutId: number | null = null
   return (...args: any[]): void => {
      if (timeoutId !== null)
         window.clearTimeout(timeoutId)

      timeoutId = window.setTimeout(() => {
         callback.apply(null, args)
      }, wait)
   }
}