-
-
Save savokiss/67fea7d9d32f76f3b03288856dbeb5f5 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