Skip to curriculum
Jump to the editor

15.3. Debounce a search input

Easy
Topics

This question is in

15. Debounce and throttle
15–20 min

A search box that asks the server on every keystroke sends five requests for "react", and the answers can arrive out of order, so the results for "rea" may replace the results for "react". A good search box waits until the user pauses, skips requests that would change nothing, and ignores answers that are no longer the latest.

Write connectSearch(input, search, render, wait) in search.js. The page (index.html, read-only) wires it to a pretend fruit search so you can try it.

Signature

JAVASCRIPT
function connectSearch(input, search, render, wait = 300) // returns disconnect()
  • input: a text input element.
  • search(query): returns a promise of an array of results.
  • render(results): shows an array of results.
  • wait: milliseconds of pause in typing before searching.
  • Returns disconnect(), which undoes the connection.

Requirements

  1. Listen for the input's input events. Each one restarts a wait of wait ms (300 when missing); nothing is searched while the user keeps typing.
  2. When a whole wait passes, read the input's value and trim it. That is the query.
  3. An empty query calls render([]) and does not search.
  4. A query equal to the last query handled (including an empty one) does nothing.
  5. Otherwise call search(query) once and render its results when they arrive, unless a newer query has been handled since; an older answer is then ignored. Clearing the box counts as a newer query.
  6. A failed search renders nothing and causes no unhandled rejection; later searches still work.
  7. disconnect() stops listening, cancels a waiting search, and makes answers still on their way stale.
  8. Each call to connectSearch has its own timer and state.

Examples

JAVASCRIPT
const input = document.querySelector("#search");
connectSearch(input, (query) => fetchResults(query), showResults);

// The user types "r", "re", "rea" 100 ms apart, then stops:
// one search for "rea", 300 ms after the last key.
JAVASCRIPT
// The user searches "lemon", then quickly types "lemons" and deletes the "s":
// the query is "lemon" again, the same as the last one: no new request.
JAVASCRIPT
// "slow" is searched and takes 1 s; "fast" is searched 300 ms later and takes 50 ms.
// render(["fast…"]) runs; the late answer for "slow" is ignored.
JAVASCRIPT
const disconnect = connectSearch(input, search, render);
disconnect(); // typing no longer searches, and nothing waiting will render

Environment

  • Plain JavaScript in a browser page; the tests call the global connectSearch from search.js.
  • The tests create their own inputs, type by setting value and dispatching an input event, and use a fake search on a virtual clock that replaces setTimeout and clearTimeout.
  • Showing a loading state, and cancelling the request itself with AbortController, are out of scope.

Hints

  1. Where does the timer live so that every input event can cancel the one before?
  2. How can an answer know, when it finally arrives, whether it is still the newest?
  3. removeEventListener needs the very same function that was added. Where is it kept?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…