Created
January 16, 2017 20:35
-
-
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)
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
// ----------------------------------- | |
// 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