Created
February 6, 2022 17:52
-
-
Save joeyguerra/fec7d56dd95ee7a3ccc8b0446841b358 to your computer and use it in GitHub Desktop.
Debounce in javascript.
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
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