logo@krutoo/utils

debounce

Returns debounced version of given function. The result function, when called, will only call the original function when timeout is reached.

The most common place to use debounces in the frontend is a request to the server when entering characters in the search field

import { debounce } from '@krutoo/utils';

function fetchSuggestions(query) {
  // ...suggestion request
}

const handleInput = debounce(event => {
  fetchSuggestions(event.target.value);
}, 500);

// fetchSuggestions will be executed 500ms after user has stopped typing
field.addEventListener('input', handleInput);