Skip to content

Instantly share code, notes, and snippets.

@rla
Created March 27, 2017 13:52
Show Gist options
  • Save rla/6c51a69041ec1c4ab8beeefd69aae1dc to your computer and use it in GitHub Desktop.
Save rla/6c51a69041ec1c4ab8beeefd69aae1dc to your computer and use it in GitHub Desktop.
Debounce
const assert = require('assert');
// Helper to debounce function calls.
module.exports = (time, fn) => {
assert.equal(typeof time, 'number');
assert.equal(typeof fn, 'function');
let debounce = false;
return function() {
if (debounce) {
return;
}
debounce = true;
setTimeout(() => {
debounce = false;
}, time);
fn.call(this);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment