Created
May 30, 2013 19:03
-
-
Save kirstein/5680273 to your computer and use it in GitHub Desktop.
Throttle events
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
| function throttle(callback, throttleTimeout, throttleTicks) { | |
| throttleTimeout = throttleTimeout || 50; | |
| throttleTicks = throttleTicks || 20; | |
| var tick = 0, | |
| timeout; | |
| return function() { | |
| var args = arguments; | |
| tick = (tick + 1) % throttleTicks; | |
| clearTimeout(timeout); | |
| if (tick === 0) { | |
| callback.apply(null, arguments); | |
| } else { | |
| timeout = setTimeout(function() { | |
| callback.apply(null, args); | |
| }, throttleTimeout); | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment