Skip to content

Instantly share code, notes, and snippets.

Created June 14, 2017 02:19
Show Gist options
  • Save anonymous/a2de5eee0f952662fcc2b8e6dd1bd138 to your computer and use it in GitHub Desktop.
Save anonymous/a2de5eee0f952662fcc2b8e6dd1bd138 to your computer and use it in GitHub Desktop.
JavaScript debounce
// from https://github.com/benfrain/rwd/blob/master/ch3/example_03-02/main.js
// This debounce function (via: https://remysharp.com/2010/07/21/throttling-function-calls) merely stops functioned firing too often on repetitive events (such as resize/scroll)
function debounce(fn, delay) {
var timer = null;
return function () {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
};
}
// usage
// removing the class from the body inside a debounce
var debouncedA = debounce(function() {
theBody.classList.remove("OffCanvas-Active");
}, 250);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment