Created
October 15, 2012 18:03
-
-
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
This file contains 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
//// 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