Created
January 3, 2018 12:46
-
-
Save Khangeldy/4a6d2bdfe60c6249b0139812490c8432 to your computer and use it in GitHub Desktop.
debouncing
This file contains 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.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); | |
} | |
} |
This file contains 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
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