Created
September 13, 2013 00:38
-
-
Save adamrights/6545634 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function SomeObject() { | |
var self = this; | |
this.lastExecThrottle = 500; // limit to one call every "n" msec | |
this.lastExec = new Date(); | |
this.timer = null; | |
this.resizeHandler = function() { | |
var d = new Date(); | |
if (d-self.lastExec < self.lastExecThrottle) { | |
// This function has been called "too soon," before the allowed "rate" of twice per second | |
// Set (or reset) timer so the throttled handler execution happens "n" msec from now instead | |
if (self.timer) { | |
window.clearTimeout(self.timer); | |
} | |
self.timer = window.setTimeout(self.resizeHandler, self.lastExecThrottle); | |
return false; // exit | |
} | |
self.lastExec = d; // update "last exec" time | |
// At this point, actual handler code can be called (update positions, resize elements etc.) | |
// self.callResizeHandlerFunctions(); | |
} | |
} | |
var someObject = new SomeObject(); | |
window.onresize = someObject.resizeHandler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment