15.6. Choose debounce or throttle for six inputs
Topics
This question is in
15. Debounce and throttleDebounce 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:
{ 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 onceThe wrappers behave like this:
- debounce: the handler runs
waitms after the last event, with the latest event. WithmaxWait, the first event after a run also starts a second timer ofmaxWaitms, and whichever timer fires first runs the handler. - throttle: an event while idle opens a window of
waitms and, withleading, runs the handler at once. Withtrailing, the latest event during the window runs when the window closes, and that run opens a new window. Withouttrailing, those events are dropped. - none: no wrapper.
The inputs and their requirements
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.
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.
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.
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.
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.
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
- Every entry's
useis"debounce","throttle"or"none", and every debounce and throttle has a positivewait. - 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:
loadMore: { use: "throttle", wait: 2000, trailing: false }A choice for a live character counter under a text box, which must update on every key:
counter: { use: "none" }Environment
- Only
plan.jsis 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
- Does the input need to react during the stream of events, or only after it?
- 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