Last active
December 20, 2016 14:09
-
-
Save srph/26fb780b84d6e1f183cd4749231172ba to your computer and use it in GitHub Desktop.
JS: Throttle
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(cb, ms) { | |
| var flag = false; | |
| return function() { | |
| var context = this; | |
| var args = arguments; | |
| if ( flag ) { | |
| return; | |
| } | |
| flag = true; | |
| cb.apply(context, args); | |
| setTimeout(function() { | |
| flag = false; | |
| }, ms); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment