I am trying to run a debounce function that take the input of "SaveValue" which would take the value of the input and save that some where. Problem is it runs the "SaveValue" function multiple time one the debounce timeout in complete. I have many of these input and don't really want to be putting a bunch of onEventListeners through out the code. What is the best way to make this work.
<input id="testInput" onkeyup="debounce(() => { SaveValue(value, 'savehere', 'onhere') } , 300)();" type="text" />
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
function SaveValue(value1, value2, value3){
console.log(`Save value of ${value1} here -> ${value2} on field -> ${value3}`);
}