Last active
August 29, 2015 14:10
-
-
Save Ficik/ac162ab2b38c373381c6 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
| /** | |
| * Calls last function passed in after {timout} ms | |
| * from last passed function | |
| * usefull for expensive typeahead and searches | |
| */ | |
| var throttle = function(timeout){ | |
| var lastCalled; | |
| var lastFn; | |
| var checking = false; | |
| return function(fn){ | |
| lastCalled = +new Date(); | |
| lastFn = fn; | |
| if (!checking){ | |
| checking = true; | |
| var check = function(){ | |
| if (lastCalled + timeout < +new Date()){ | |
| lastFn(); | |
| checking = false; | |
| } else { | |
| setTimeout(function(){ check(); }, timeout); | |
| } | |
| }; | |
| setTimeout(function(){ check(); }, timeout); | |
| } | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment