Skip to content

Instantly share code, notes, and snippets.

@bryanveloso
Forked from simonw/ratelimit.js
Created February 2, 2010 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanveloso/293019 to your computer and use it in GitHub Desktop.
Save bryanveloso/293019 to your computer and use it in GitHub Desktop.
/* Use this to cause a function to fire no more than once every 'ms' milliseconds.
For example, an expensive mousemove handler:
$('body').mouseover(ratelimit(function(ev) {
// ...
}, 250));
*/
function ratelimit(fn, ms) {
var last = (new Date()).getTime();
return (function() {
var now = (new Date()).getTime();
if (now - last > ms) {
last = now;
fn.apply(null, arguments);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment