Last active
January 13, 2017 18:50
-
-
Save andreasvirkus/7aea1c59cdc87736117e7e6468cb7659 to your computer and use it in GitHub Desktop.
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
// Another poor man's vanilla throttle script | |
window.addEventListener('scroll', throttle(callback, 1000)); | |
// v1 | |
function throttle(fn, wait) { | |
var time = Date.now(); | |
return function() { | |
if (time + wait - Date.now() < 0) { | |
fn(); | |
time = Date.now(); | |
} | |
}; | |
} | |
// v2 | |
function throttleTwo(callback, delay) { | |
var timeout; | |
return function() { | |
var context = this, | |
args = arguments; | |
clearTimeout(timeout); | |
timeout = setTimeout(function() { | |
callback.apply(context, args); | |
}, delay); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment