Can someone help me understand how this code snippet is going to work?
In geeks from greeks they have following code snippet
<html>
<body>
<button id="debounce">
Debounce
</button>
<script>
var button = document.getElementById("debounce");
const debounce = (func, delay) => {
let debounceTimer
return function() {
const context = this
const args = arguments
clearTimeout(debounceTimer)
debounceTimer
= setTimeout(() => func.apply(context, args), delay)
}
}
button.addEventListener('click', debounce(function() {
alert("Hello\nNo matter how many times you" +
"click the debounce button, I get " +
"executed once every 3 seconds!!")
}, 3000));
</script>
</body>
</html>
From the following Difference Between throttling and debouncing a function, it says, emit only if there is no pending action (and after specific second) Answer
Here I can't see any if condition to check if there is a pending action.
From the article they say
> If the debounce button is clicked only once, the debounce function
> gets called after the delay. However, if the debounce button is
> clicked once, and again clicked prior to the end of the delay, the
> initial delay is cleared and a fresh delay timer is started. The
> clearTimeout function is being used to achieve it.
So everytime the button is clicked, the timer is cleared and a new timer is started (and probably the new action is also triggered), which probably doesn't sound anything like what I read from SFO answer
Also, Can someone please also explain how func.apply(context, args), delay)
is used or code-line by line?