Created
June 14, 2017 02:19
-
-
Save anonymous/a2de5eee0f952662fcc2b8e6dd1bd138 to your computer and use it in GitHub Desktop.
JavaScript 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
// 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