Created
December 11, 2014 03:47
-
-
Save zhoufenfens/268a4b62d9b05e686622 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 throttle = function(func, wait) { | |
var context, args, timeout, result, previous, later; | |
previous = 0; | |
later = function() { | |
previous = new Date(); | |
timeout = null; | |
result = func.apply(context, args); | |
}; | |
return function() { | |
var now = new Date(), remaining = wait - (now - previous); | |
context = this; | |
args = arguments; | |
if (remaining <= 0) { | |
clearTimeout(timeout); | |
timeout = null; | |
previous = now; | |
result = func.apply(context, args); | |
} else if (!timeout) { | |
timeout = setTimeout(later, remaining); | |
} | |
return result; | |
}; | |
} |
Author
zhoufenfens
commented
Dec 11, 2014
first | second |
---|---|
1 | 2 |
3 | 4 |
🎎
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment