Skip to content

Instantly share code, notes, and snippets.

@jeffersonmourak
Last active November 16, 2017 21:39
Show Gist options
  • Select an option

  • Save jeffersonmourak/c35a7241868c55d2e56e7d5049f0d4a7 to your computer and use it in GitHub Desktop.

Select an option

Save jeffersonmourak/c35a7241868c55d2e56e7d5049f0d4a7 to your computer and use it in GitHub Desktop.
class Debounce {
constructor(fn, wait, accumulator) {
this.fn = fn;
this.wait = wait || 500;
this.accumulator = accumulator;
this.store = '';
this.timeout;
}
call() {
let args = arguments;
if (this.accumulator) {
this.store = this.accumulator.apply(this, [this.store, ...arguments]);
args = [this.store];
}
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
this.fn.apply(this, args);
this.store = "";
}, this.wait);
}
}
module.exports = Debounce;

como usar.

Inicie a class

let deb = new Debounce(fn, time)

chame o método call

deb.call(ar,gu,ment,os);

e pronto.

Você ainda pode adicionar um acumulador pra salvar as chamadas

let deb = new Debounce(fn, time, acumulador)

function acumulador(acumulado, argumento) {
  return acumulador + argumento;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment