Skip to content

Instantly share code, notes, and snippets.

@Khangeldy
Created January 3, 2018 12:46
Show Gist options
  • Save Khangeldy/4a6d2bdfe60c6249b0139812490c8432 to your computer and use it in GitHub Desktop.
Save Khangeldy/4a6d2bdfe60c6249b0139812490c8432 to your computer and use it in GitHub Desktop.
debouncing
Function.prototype.debounce = function (delay) {
var func = this,
timeout;
return function debounced() {
var obj = this,
args = arguments;
function delayed () {
func.apply(obj, args);
timeout = null;
};
if (timeout) clearTimeout(timeout);
timeout = setTimeout(delayed, delay || 100);
}
}
var timeout;
function callback() {
console.log(window.scrollY)
}
function delayCallback() {
if(timeout)
clearTimeout(timeout)
timeout = window.setTimeout(callback, 100)
}
document.addEventListener('scroll', delayCallback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment