Skip to content

Instantly share code, notes, and snippets.

@joeyguerra
Created February 6, 2022 17:52
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 joeyguerra/fec7d56dd95ee7a3ccc8b0446841b358 to your computer and use it in GitHub Desktop.
Save joeyguerra/fec7d56dd95ee7a3ccc8b0446841b358 to your computer and use it in GitHub Desktop.
Debounce in javascript.
import assert from 'assert';
describe('Debounce', ()=>{
it('When called super fast, then should only increment once', done=>{
const debounce = timer => (fn, timeout) => {
clearTimeout(timer);
timer = setTimeout(fn, timeout);
return timer;
};
let timer = null
let actual = 0;
[1,2,3,4,5].forEach(()=>{
timer = debounce(timer)(()=>{
actual++;
}, 10);
});
setTimeout(()=>{
assert.deepEqual(actual, 1);
done();
}, 10);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment