Created
April 2, 2014 08:05
-
-
Save joladev/9929890 to your computer and use it in GitHub Desktop.
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
// Throttle from underscore.js, simplified and deattached | |
var throttle = function(func, wait) { | |
var context, args, result; | |
var timeout = null; | |
var previous = 0; | |
var later = function() { | |
previous = Date.now(); | |
timeout = null; | |
result = func.apply(context, args); | |
context = args = null; | |
}; | |
return function() { | |
var now = Date.now(); | |
if (!previous) 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) { | |
timeout = setTimeout(later, remaining); | |
} | |
return result; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment