Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Understanding Debounce in JavaScript: A Guide with Examples

Learn how to implement and use the debounce function to optimize performance when handling rapid events like scrolling, resizing, and key presses. When dealing with events that fire rapidly—like window resizing, scrolling, or key p…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

Learn how to implement and use the debounce function to optimize performance when handling rapid events like scrolling, resizing, and key presses.



When dealing with events that fire rapidly—like window resizing, scrolling, or key presses—it’s crucial to optimize performance and prevent unnecessary function calls. That’s where debounce comes in.






What is Debouncing?



Debouncing is a technique that ensures a function is executed only after a specified delay from the last time it was called. This prevents excessive executions and improves performance, especially in scenarios like:




  • Handling window resize events efficiently.

  • Reducing API calls when a user types in a search box.

  • Controlling scroll events to avoid excessive computations.






Implementing a Debounce Function



Here’s a simple yet effective debounce function in JavaScript:




/**
* Creates a debounced version of the given function.
* The debounced function delays invoking `func` until after
* it stops being called for `wait` milliseconds.
* Useful for reducing the frequency of execution on rapid events.
*
* @param {Function} func - The function to debounce.
* @param {number} wait - The delay in milliseconds.
* @returns {Function} A debounced version of `func`.
*/

export function debounce(func, wait) {
let timeout;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), wait);
};
}









How It Works




  1. The function takes two parameters:



    • func: The function to be executed after the delay.


    • wait: The delay in milliseconds.



  2. It returns a new function that clears any previously set timeout and sets a new one.

  3. If the returned function is called before the wait time is over, the previous timer is cleared and restarted.

  4. Once the wait time has elapsed without further calls, func is executed with the original arguments.






Practical Examples






1️⃣ Debouncing a Window Resize Event






function handleResize() {
console.log('Window resized at', new Date().toLocaleTimeString());
}

// Create a debounced version of the resize handler with a 250ms delay
const debouncedHandleResize = debounce(handleResize, 250);
window.addEventListener('resize', debouncedHandleResize);






Why?

Without debouncing, the resize event fires continuously as the user resizes the window. With debounce, the function executes only after the user stops resizing.






2️⃣ Debouncing Scroll Events






function onScroll() {
console.log('Scroll processed at:', new Date().toLocaleTimeString());
}

const debouncedScroll = debounce(onScroll, 100);
window.addEventListener('scroll', debouncedScroll);






Why?

Scrolling fires multiple times per second. Debouncing ensures that the onScroll function executes only after the user stops scrolling for 100ms.






3️⃣ Debouncing Key Presses in an Input Field






<input type="text" id="myInput" placeholder="Type something..." />









function onKeyPress(event) {
console.log(`Key "${event.key}" pressed at:`, new Date().toLocaleTimeString());
}

const debouncedKeyPress = debounce(onKeyPress, 300);
document.getElementById('myInput').addEventListener('keyup', debouncedKeyPress);






Why?



If you attach an API call to a keypress event, you don’t want it to trigger every single time a user types a character. Instead, debounce ensures that the API call is made only after the user stops typing for 300ms.






When Should You Use Debounce?



Use debounce when dealing with high-frequency events that can cause performance issues, such as:




  • Window resizing (resize events)

  • Scrolling (scroll events)

  • Typing (keyup, keydown events)

  • Auto-saving features in forms

  • Live-search functionalities






Conclusion



Debounce is a powerful tool to optimize event handling in JavaScript. By ensuring that functions execute only after a delay, we can significantly reduce unnecessary computations and improve performance. Whether it’s handling user input, scrolling, or window resizing, debounce helps ensure smooth and efficient interactions.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Understanding Debounce in JavaScript: A Guide with Examples

Thematisch verwandte Begriffe: Understanding, Debounce, JavaScript, Guide · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-79918 | MaxKB is an open-source AI assistant for enterprise. Prior to version 2.…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick