Last active
January 4, 2016 07:49
-
-
Save kylesuss/8590833 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
var _ = { | |
now: Date.now || function() { return new Date().getTime(); }, | |
throttle: function(func, wait, options) { | |
var context, args, result; | |
var timeout = null; | |
var previous = 0; | |
options || (options = {}); | |
var later = function() { | |
previous = options.leading === false ? 0 : _.now(); | |
timeout = null; | |
result = func.apply(context, args); | |
context = args = null; | |
}; | |
return function() { | |
var now = _.now(); | |
if (!previous && options.leading === false) previous = now; | |
var remaining = wait - (now - previous); | |
context = this; | |
args = arguments; | |
if (remaining <= 0) { | |
clearTimeout(timeout); | |
timeout = null; | |
previous = now; | |
result = func.apply(context, args); | |
context = args = null; | |
} else if (!timeout && options.trailing !== false) { | |
timeout = setTimeout(later, remaining); | |
} | |
return result; | |
}; | |
} | |
} | |
// Scroll event | |
// var throttled = _.throttle(someCallback, 200); | |
// $(window).scroll(throttled); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment