Skip to content

Instantly share code, notes, and snippets.

@jakekara
Created January 16, 2017 20:35
Show Gist options
  • Save jakekara/b5b841b780c693377ff151019d668c88 to your computer and use it in GitHub Desktop.
Save jakekara/b5b841b780c693377ff151019d668c88 to your computer and use it in GitHub Desktop.
Barebones object for throttling function calls (preventing them from being called too often)
// -----------------------------------
// barebones example of how to throttle a function
// so it doesn't fire too frequently
var THROTTLE = function(){
this.wait = 200;
}
// execute f in this.wait milliseconds. if there's already an f waiting to be called,
// drop it and replace it with this f
THROTTLE.prototype.go = function(f){
clearTimeout(this.timeout);
this.timeout = setTimeout(f, this.wait);
return this;
}
// -----------------------------------
// example usage:
// ...
this.throttle = new THROTTLE();
// ...
var that = this;
d3.select(window).on("resize",
function(){
that.throttle.go(function(){
that.draw.call(that);
});
});
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment