Skip to main content

What is debounce

· 3 min read
Phan Thanh Sang
Front End Engineer @ Sea Limited

Debouncing is a technique used in electronics and software development to ensure that only one signal or event is registered despite multiple signals or events occurring. It's commonly applied in situations where mechanical contacts (like buttons or switches) are involved, which can generate multiple signals due to bouncing.

How Debouncing Works:

  1. Mechanical Switches and Bouncing: When a mechanical switch (like a button) is pressed or released, the contacts may not make a clean, single transition from open to closed (or vice versa). Instead, they can briefly bounce between open and closed states before settling.

  2. Multiple Signal Transitions: This bouncing can lead to the detection of multiple signal transitions (e.g., multiple button presses or releases) even if the user intended to perform only one action.

  3. Debouncing Process: To address this issue, a debouncing mechanism is used:

  • Hardware Debouncing: In hardware, this can involve using capacitors, resistors, or specialized integrated circuits (ICs) to filter out rapid changes and ensure only one clean signal transition is registered.

  • Software Debouncing: In software, particularly in microcontroller or computer programming, debouncing is achieved by adding a delay or timer mechanism:

    • Simple Delay: Wait for a short period after detecting a change before accepting further changes. This assumes the bouncing settles within that time.
    • State Change Detection: Keep track of the switch state over time and only register a state change when it remains stable for a certain period.

Demo

Last click at -- not yet :<
Debounce update date 2 seconds -- not yet :<

Implementation

Here is a simple implementation of debounce in Javascript.

function debounce(callback, delayMilliseconds) {
let timer;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => {
callback(...args);
}, delayMilliseconds);
};
}

You have a timer variable in closer to hold the timer id; the debounce function return a function to be called. When user call the function returned by debounce, a timer is initiated. The timer will decay based on the delay in millisecond. After the delay, if there is no invoke of the returned function the callback will be fired. If however there is at least 1 invoke within the delay, the timer will be clear and refresh the timer. The process is going to be continued until the function not being called within the given delay and the callback eventually fire.

Importance of Debouncing:

  • Prevents Unintended Multiple Actions: Without debouncing, a single button press could be interpreted as multiple presses, leading to unintended consequences in software applications or erratic behavior in electronic circuits.
  • Improves User Experience: Ensures that user interactions with buttons or switches are reliable and predictable, enhancing the overall usability of devices or applications.

Debouncing is a fundamental concept in both hardware design and software development whenever mechanical switches or contacts are used to interface with digital systems.