Skip to content

Instantly share code, notes, and snippets.

@Ficik
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save Ficik/ac162ab2b38c373381c6 to your computer and use it in GitHub Desktop.

Select an option

Save Ficik/ac162ab2b38c373381c6 to your computer and use it in GitHub Desktop.
/**
* 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