Skip to content

Instantly share code, notes, and snippets.

@marksteve
Last active August 29, 2015 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marksteve/66f376b8db6c4625957c to your computer and use it in GitHub Desktop.
Save marksteve/66f376b8db6c4625957c to your computer and use it in GitHub Desktop.
Decorator to limit function calls to every `cd` ms
var throttle = function(f, cd) {
var _cd = new Date;
return function() {
if (_cd - new Date < 0) {
_cd = new Date(new Date().getTime() + cd);
return f.apply(this, arguments);
}
};
};
/*
var shout = throttle(function() { console.log('SHOUT!'); }, 1000);
setInterval(shout, 100);
*/
@rstacruz
Copy link

try +new Date() instead of .getTime() :) or even cd + new Date()

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