Debounce and Throttling

Debounce and Throttling

Question : How does the JavaScript code implement debounce functionality for button clicks?

Explanation: The JavaScript code uses lodash's _.debounce function to create a debounced version of a callback function. It tracks the number of times a button is pressed and updates the count after a specified debounce time (800ms in this case).

const btn = document.querySelector(".Increment_btn");
const btnPress = document.querySelector(".increment_pressed");
const count = document.querySelector(".increment_count");

var pressedCount = 0;
var triggeredCount = 0;

 const debouncedCount = _.debounce(() => {
   count.innerHTML = ++triggeredCount;
 }, 800);

 btn.addEventListener("click", () => {
   btnPress.innerHTML = ++pressedCount;
   debouncedCount();
 });
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Debouncing and Throttling</title>
</head>
<body>
    <div>
        <h1>Subscribe Me <br/> else you wil have bad dreams today</h1>
    </div>

    <button class="Increment_btn">Increment</button>
    <p>Button Pressed <span class="increment_pressed">0</span> Times</p>
    <p>Triggered <span class="increment_count">0</span> Times</p>

    <-- here it is cdn for lodash --> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> 

    <script type="text/javascript" src="debounsing.js"></script>
</body>
</html>

Question: How does the JavaScript code implement throttle functionality for button clicks?

Explanation: The JavaScript code uses lodash's _.throttle function to create a throttled version of a callback function. It tracks the number of times a button is pressed and updates the count at intervals of 1000ms, ensuring that the callback function is not invoked more than once in that interval.

const btn = document.querySelector(".Increment_btn");
const btnPress = document.querySelector(".increment_pressed");
const count = document.querySelector(".increment_count");



 const throttleCount = _.throttle(() => {
  count.innerHTML = ++triggeredCount;
}, 800);

 btn.addEventListener("click", () => {
   btnPress.innerHTML = ++pressedCount;
   throttleCount();
 });

Question: What is the purpose of the JavaScript code for a custom debounce function (polyfill)?

Explanation: The JavaScript code defines a custom debounce function myDebounce that mimics the behavior of lodash's _.debounce. It creates a debounced version of a callback function and ensures that the callback is executed only after a specified debounce time (800ms in this case) since the last invocation.

const myDebounce = (cb, d) => {
  let timer;
  return function (...args) {
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => {
      cb(...args);
    }, d);
  };
};

const debouncedCount = myDebounce(() => {
  triggeredCount += 1;
  count.innerHTML = triggeredCount;
}, 800);

btn.addEventListener("click", () => {
  btnPress.innerHTML = ++pressedCount;
  debouncedCount();
});

Question: What is the purpose of the JavaScript code for a custom throttle function (polyfill)?

Explanation: The JavaScript code defines a custom throttle function myThrottle that mimics the behavior of lodash's _.throttle. It creates a throttled version of a callback function and ensures that the callback is invoked at most once per specified interval (1000ms in this case), preventing rapid successive invocations.


const myThrottle = (cb, d) => {
  let last = 0;
  return now = new Date   

};

const throttled = myThrottle(() => {
  triggeredCount += 1;
  count.innerHTML = triggeredCount;
}, 1000);

btn.addEventListener("click", () => {
  btnPress.innerHTML = ++pressedCount;
  throttled();
});

![](https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=abhishekdandriyal&button_colour=FFDD00&font_colour=000000&font_family=Cookie&outline_colour=000000&coffee_colour=ffffff align="left")