Skip to content

Instantly share code, notes, and snippets.

@bhattisatish
Created October 15, 2012 18:03
Show Gist options
  • Save bhattisatish/3894036 to your computer and use it in GitHub Desktop.
Save bhattisatish/3894036 to your computer and use it in GitHub Desktop.
Limiting JS Function calls to avoid frequent calls. Origin http://ryhan.org/post/33374611308/limiting-function-calls
//// some function
//var foo = function ( data ){ console.log(data); }
//// identical to foo(), except can only be called once every 100ms.
//var limitedFoo = trickle(foo, 100);
////
function trickle( fun , waitLength )
{
var lastCalledAt = 0,
context = this;
return function(){
var now = (new Date()).getTime();
if (now > lastCalledAt + waitLength )
{
lastCalledAt = now;
fun.apply(context, arguments);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment