Skip to content

Instantly share code, notes, and snippets.

@simonw
Created February 2, 2010 10:27
Show Gist options
  • Save simonw/292562 to your computer and use it in GitHub Desktop.
Save simonw/292562 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);
}
});
}
@GarrettS
Copy link

I have published this strategy several times as an improvement over Zakas' throttle function, which was invented by Richard Cornford.

http://lists.w3.org/Archives/Public/www-style/2012Oct/0192.html
http://www.highdots.com/forums/1255048-post42.html

@EdgarIvanEspinoza
Copy link

Que crack mi bro 🔥🔥🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment