Skip to curriculum
Jump to the editor

15.6. Choose debounce or throttle for six inputs

Easy
Topics

This question is in

15. Debounce and throttle
15–20 min

Debounce and throttle both limit how often a handler runs, but they answer different questions. Debounce waits for the events to stop, then runs once. Throttle runs at a steady rate while the events keep coming. Some handlers need neither. Picking the wrong one gives a search that fires on every key, a progress bar that freezes while scrolling, or a pay button that charges twice.

In plan.js, choose a wrapper and its settings for each of the six inputs below. The tests replay each input's events through your choice and check its requirements.

Files

plan.js holds one entry per input:

JAVASCRIPT
{ use: "debounce", wait: 300 }                  // optional: maxWait
{ use: "throttle", wait: 100 }                  // optional: leading, trailing (true by default)
{ use: "none" }                                 // the handler runs on every event, at once

The wrappers behave like this:

  • debounce: the handler runs wait ms after the last event, with the latest event. With maxWait, the first event after a run also starts a second timer of maxWait ms, and whichever timer fires first runs the handler.
  • throttle: an event while idle opens a window of wait ms and, with leading, runs the handler at once. With trailing, the latest event during the window runs when the window closes, and that run opens a new window. Without trailing, those events are dropped.
  • none: no wrapper.

The inputs and their requirements

  1. search: a search box. Eight keys 90 ms apart, a pause, then three more keys.
    • One request per pause in typing (2 requests), each with the text at the pause.
    • Results are asked for within 400 ms of the last key.
  2. autosave: a note editor. A key every 150 ms for 12 seconds, without a pause.
    • Every keystroke is saved within 5 seconds.
    • The final text is saved within 1.5 s after the last key.
    • At most 4 saves in all.
  3. scroll: a reading progress bar. A scroll event every 10 ms for 3 seconds.
    • The bar updates at least every 200 ms while scrolling, and at most once per 50 ms.
    • The final position is shown within 200 ms.
  4. arrows: a puzzle game. Each arrow-key press moves the piece one square; two presses can land in the same millisecond.
    • Every press moves the piece at once: none dropped, merged or delayed.
  5. pay: a "Pay" button. Clicks at 0, 300, 700, 1500, 1600 and 3000 ms.
    • The first click pays at once.
    • No second payment within 1 s after a payment, and nothing pays later on its own.
    • A click more than 1 s after the last payment pays at once: payments at 0, 1500 and 3000 ms.
  6. resize: a costly layout. Two window resizes, each a drag with an event every 16 ms.
    • The layout runs once per drag, with the final size, within 500 ms after the drag ends.

Requirements

  1. Every entry's use is "debounce", "throttle" or "none", and every debounce and throttle has a positive wait.
  2. Each input's requirements above hold when its events are replayed through your choice.

Examples

A choice for a different input, a "Load more" button that must not send a request twice within 2 seconds but must react to the first click:

JAVASCRIPT
loadMore: { use: "throttle", wait: 2000, trailing: false }

A choice for a live character counter under a text box, which must update on every key:

JAVASCRIPT
counter: { use: "none" }

Environment

  • Only plan.js is edited. The tests replay the events on a virtual clock, so nothing waits for real time.
  • More than one setting can pass an input's requirements; any that passes is accepted.

Hints

  1. Does the input need to react during the stream of events, or only after it?
  2. For the pay button, what should happen to the clicks that come too soon: run later, or never?

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…