JSFiddle - React, Tailwind, and code Playground

by Ajey

HTML

<button id="btn">Click Me FAST!!!</button>

JavaScript

function debounce(cb, delay) {
  let timer = null;

  return function() {
    let context = this;
    let args = arguments;

    clearTimeout(timer);

    timer = setTimeout(function() {
      cb.apply(context, args);
    }, delay);
  }
}

function test() {
  console.log("Button clicked");
}

// Example of Click handler WITH Debounce 
document.addEventListener('click', debounce(test, 2000));

// Example of Click handler WITHOUT Debounce 
// document.addEventListener('click', test);