I built one JavaScript debounce function, I need JavaScript expert's opinion if this is the correct way to do it and if not then what is the flaw in this current function. Thanks in advance for your opinion this will help me to learn.
var debounce = function(inpFun, wait) {
var timeout;
return function () {
if(!timeout) {
inpFun.apply(this, arguments);
timeout = setTimeout(function() {
timeout = undefined;
}, wait);
}
else {
console.log("Debouncing");
}
}
};
var buttonClickFunction = debounce(function (event) {
console.log("Button Clicked");
console.log(event.target.id);
}, 2000);
document.querySelector("#button1").addEventListener("click", buttonClickFunction);
<button id="button1">Button</button>