Logesh Paul
  • Welcome
  • About
  • đź“–Wiki
    • Productivity
      • My Ultimate Self Guide - For Better Living
      • Finding your Passion, Purpose of Life
      • Great Techniques To Self Mastery
      • Better Yields Best
      • Who is Driving? You (or) Your Mind
      • Goals
      • My Workdesk (Sep 2024)
      • SublimeText Plugins  —  I Use
    • Code
      • Bash
      • Regex
      • Capybara
      • Chrome
      • CSS
      • Git
      • Mac
      • Atom
      • Ruby
      • Slack
      • Husky
      • Zsh
      • Javascript
      • Best Practices
      • What is debounce?
      • Rbenv
      • Brew
      • RVM
    • Design
      • Why do we need a design system?
    • Health
      • Facts about getting fit
      • TDS and its Role in Drinking Water
      • Gut
    • Mind
      • Behaviour
      • Evolving Brain
      • The Law of Wasted Effort
    • Personal
      • Vision and Values
      • Learning From My First Startup
    • Finance
      • 5 Finance Advices From The Ancient Richest City Of The World
      • Interim Dividend
      • Cash Reserve Ratio
      • Dematerialisation
Powered by GitBook
On this page
  1. Wiki
  2. Code

What is debounce?

PreviousBest PracticesNextRbenv

Last updated 2 years ago

What is debounce?

The term debounce comes from electronics. When you’re pressing a button, let’s say on your TV remote, the signal travels to the microchip of the remote so quickly that before you manage to release the button, it bounces, and the microchip registers your “click” multiple times.

To mitigate this, once a signal from the button is received, the microchip stops processing signals from the button for a few microseconds while it’s physically impossible for you to press it again.

function debounce(func, timeout = 300){
  let timer;
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => { func.apply(this, args); }, timeout);
  };
}
function saveInput(){
  console.log('Saving data');
}
const processChange = debounce(() => saveInput());
function debounce(func, timeINMS) {
  let timeout;

  return function () {
    clearTimeout(timeout);
    timeout = setTimeout(func, timeINMS);
  };
}

let debouncedHello = debounce(() => console.log("say hello", Date.now()), 800);

đź“–
Codepen