Last active
November 20, 2020 20:11
-
-
Save adamjgrant/c5c7c9b553a7c90953871c8c7ec89591 to your computer and use it in GitHub Desktop.
debounce_v2.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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