Last active
August 19, 2018 18:14
-
-
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/
This file contains hidden or 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
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