Skip to content

Instantly share code, notes, and snippets.

@ldmarz
Last active August 19, 2018 18:14
Show Gist options
  • Save ldmarz/0e2ba1e99e3bba7f0af3ced34d74036c to your computer and use it in GitHub Desktop.
Save ldmarz/0e2ba1e99e3bba7f0af3ced34d74036c to your computer and use it in GitHub Desktop.
Example of how to use debounce from lodash, more information at http://ldmarz.com/improve-the-speed-of-your-javascript-code-with-debounce/
function writeDOM(e) {
// Imagine some slow/hard function
$("#result").text(`${e.clientX} - ${e.clientY}`);
}
const $checkBox = $('#checkbox');
const debouncedWriteDom = _.debounce(writeDOM, 100);
$("#container-result").mousemove(e => {
if ($checkBox.is(':checked')) {
debouncedWriteDom(e);
} else {
writeDOM(e);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment