Skip to content

Searching with debounce is a technique used to optimize search functionality in web applications. It helps reduce the number of search requests made to the server by delaying the execution of the search function until a certain amount of time has passed since the user stopped typing.

Notifications You must be signed in to change notification settings

Shahriyar-Hosen/debounce

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 

Repository files navigation

Search with Debounce

Functionality

const filterBySearch = (value: string) => {
  console.log(value);
};

const debounce = (fn: (value: string) => void, delay: number) => {
  let timeoutId: NodeJS.Timeout | number | undefined;

  return (e: FormEvent) => {
    if (timeoutId) {
      clearTimeout(timeoutId);
    }

    timeoutId = setTimeout(() => {
      fn((e.target as HTMLInputElement).value);
    }, delay);
  };
};

Input Section

<input
  className="search-input"
  type="search"
  name="search"
  placeholder="Search"
  onChange={debounce((value) => filterBySearch(value), 500)}
/>

About

Searching with debounce is a technique used to optimize search functionality in web applications. It helps reduce the number of search requests made to the server by delaying the execution of the search function until a certain amount of time has passed since the user stopped typing.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published