Skip to content

Instantly share code, notes, and snippets.

@adamjgrant
Last active November 20, 2020 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamjgrant/c5c7c9b553a7c90953871c8c7ec89591 to your computer and use it in GitHub Desktop.
Save adamjgrant/c5c7c9b553a7c90953871c8c7ec89591 to your computer and use it in GitHub Desktop.
debounce_v2.js
class Whatever {
constructor() {
this.debounceQueue = {};
}
debounce(fn, id, delay, args) {
delay = delay || 1000;
args = args || [];
if (typeof debounceQueue[id] !== "object") {
debounceQueue[id] = {};
}
if (typeof debounceQueue[id].debounceTimer !== "undefined") {
clearTimeout(debounceQueue[id].debounceTimer);
}
return debounceQueue[id] = {
fn: fn,
id: id,
delay: delay,
args: args,
debounceTimer: setTimeout(function() {
debounceQueue[id].fn.apply(this, debounceQueue[id].args);
return debounceQueue[id] = void 0;
}.bind(this), delay)
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment