0
0
Vueframework~10 mins

Debounced watchers pattern in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Debounced watchers pattern
Data changes
Watcher triggers
Start debounce timer
If new change before timer ends
Reset timer
Timer ends -> Run effect
When data changes, the watcher starts a timer. If data changes again before timer ends, timer resets. Effect runs only after no changes for set time.
Execution Sample
Vue
import { ref, watch } from 'vue'

function debounce(fn, delay) {
  let timeout;
  return function(newVal) {
    if (timeout) {
      clearTimeout(timeout);
    }
    timeout = setTimeout(() => {
      fn(newVal);
    }, delay);
  };
}

const search = ref('');

function debounceHandler(newVal) {
  console.log('Search:', newVal);
}

const debouncedHandler = debounce(debounceHandler, 300);

watch(search, debouncedHandler);
This code watches the 'search' ref and runs debounceHandler only after 300ms of no changes.
Execution Table
StepData ChangeTimer StatusActionEffect Run
1search = 'a'Timer started (300ms)Wait for debounceNo
2search = 'ab' (within 300ms)Timer reset (300ms)Wait for debounceNo
3search = 'abc' (within 300ms)Timer reset (300ms)Wait for debounceNo
4No change for 300msTimer endsRun debounceHandler('abc')Yes
5search = 'abcd'Timer started (300ms)Wait for debounceNo
6No change for 300msTimer endsRun debounceHandler('abcd')Yes
7No further changesNo timerIdleNo
💡 No further data changes, debounce timer ended, effect executed.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
search'''a''ab''abc''abc''abcd''abcd''abcd'
Timeroffon (300ms)reset (300ms)reset (300ms)endedon (300ms)endedoff
Effect RunNoNoNoNoYesNoYesNo
Key Moments - 3 Insights
Why doesn't the effect run immediately when 'search' changes?
Because the watcher uses debounce, it waits for 300ms of no changes before running the effect, as shown in steps 1-4 in the execution_table.
What happens if 'search' changes multiple times quickly?
The debounce timer resets each time, delaying the effect until changes stop, as seen in steps 2 and 3 where the timer resets.
When exactly does the debounceHandler function run?
It runs only after the timer ends with no new changes, shown in steps 4 and 6 where the effect runs after 300ms idle.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the timer status at step 3?
ATimer ended
BTimer off
CTimer reset (300ms)
DTimer started (100ms)
💡 Hint
Check the 'Timer Status' column at step 3 in the execution_table.
At which step does the effect first run?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Effect Run' column to find the first 'Yes'.
If the 'search' value changed again at step 4 before timer ended, what would happen?
ATimer resets and effect delays
BTimer stops and effect never runs
CEffect runs immediately
DEffect runs twice
💡 Hint
Refer to steps 2 and 3 where timer resets on quick changes.
Concept Snapshot
Debounced watchers delay running effects until data stops changing for a set time.
Use watch() with debounce option or manual timer.
Multiple quick changes reset the timer.
Effect runs only after no changes for debounce period.
Helps avoid running expensive code too often.
Full Transcript
In Vue, a debounced watcher waits for data to stop changing before running its effect. When the watched data changes, a timer starts. If the data changes again before the timer ends, the timer resets. Only after the data stays unchanged for the debounce time does the effect run. This pattern prevents running code too often during rapid changes, like typing in a search box. The execution table shows each data change, timer status, and when the effect runs. Variables like 'search' and the timer state update step-by-step. Key moments clarify why the effect delays and how timer resets work. The quiz tests understanding of timer status and effect timing. This pattern improves performance and user experience by reducing unnecessary work.