Debouncing

إلغاء الارتداد (Debouncing)

Intermediateprogramming1 min read
debouncethrottlethrottlingrate limit uiinput delaysearch delay

Definition

A programming technique that delays executing a function until after a user has stopped triggering it for a set time.

تقنية برمجية تُؤخّر تنفيذ دالة حتى بعد توقف المستخدم عن تشغيلها لفترة محددة.

Why It Matters

The glossary search in 404Fault should debounce the search input — without debouncing, filtering 155+ terms happens on every single keystroke, potentially freezing the browser.

بحث القاموس في 404Fault يجب أن يُلغي ارتداد مدخل البحث — بدون debouncing، تصفية 155+ مصطلح تحدث عند كل ضغطة مفتاح، مما قد يُجمّد المتصفح.

Full Definition

Debouncing delays a function call until N milliseconds after the last invocation. If the function is called again within N ms, the timer resets. Used for search inputs (don't query on every keystroke — wait until the user pauses), resize handlers, and autosave. Throttling is different — it executes at most once per N ms regardless of how often it's called. Both prevent excessive function calls.
Debouncing يُؤخّر استدعاء دالة حتى N ميلي ثانية بعد آخر استدعاء. إذا استُدعيت الدالة مجددًا خلال N ms، يُعاد ضبط المؤقت. يُستخدم لمدخلات البحث (لا تستعلم عند كل ضغطة مفتاح — انتظر حتى يتوقف المستخدم).

Example Usage

// Simple debounce hook: function useDebounce<T>(value: T, delay: number): T { const [dv, setDv] = useState(value); useEffect(() => { const t = setTimeout(() => setDv(value), delay); return () => clearTimeout(t); }, [value, delay]); return dv; } const debouncedSearch = useDebounce(search, 300);

// خطاف debounce بسيط: function useDebounce<T>(value: T, delay: number): T { const [dv, setDv] = useState(value); useEffect(() => { const t = setTimeout(() => setDv(value), delay); return () => clearTimeout(t); }, [value, delay]); return dv; } const debouncedSearch = useDebounce(search, 300);

Knowledge Graph

Avoid these mistakes when using Debouncing:

1

Not clearing the timeout on cleanup — causes state updates on unmounted components

2

Debouncing too long (>500ms) — the UI feels unresponsive; 150-300ms is usually right for search

Sign in to unlock guided AI explanations from AI Teacher.

Generate a Prompt

Copy this prompt and use it directly with any AI model — no setup needed.

Ready-to-Use Prompt
Help me build a project using Debouncing.

Explain:
1. What is Debouncing and why it matters
2. The core architecture and required tools
3. Step-by-step implementation plan
4. Common mistakes to avoid: Not clearing the timeout on cleanup — causes state updates on unmounted components, Debouncing too long (>500ms) — the UI feels unresponsive; 150-300ms is usually right for search
5. Best practices and production tips

Official Resources