Skip to content

Instantly share code, notes, and snippets.

@chris-kobrzak
Last active November 8, 2015 19:13
Show Gist options
  • Save chris-kobrzak/6b544d4b310b0543e9b0 to your computer and use it in GitHub Desktop.
Save chris-kobrzak/6b544d4b310b0543e9b0 to your computer and use it in GitHub Desktop.
Sample implementation of the browser window resize observer that throttles native browser events and emits custom events instead. This might be useful if you don't need to respond to resize events immediately and performance is a major concern.
WindowResizeManager = function( debounceWait ) {
this.debounceWait = debounceWait || 200;
_.bindAll(this, "handleWindowResizeEvent", "triggerWindowResizeCompleteEvent");
this.cacheDom();
this.bindWindowResizeEvent();
};
WindowResizeManager.prototype.cacheDom = function() {
this.window: $(window);
};
WindowResizeManager.prototype.bindWindowResizeEvent = function() {
var handler = this.handleWindowResizeEvent();
this.window.on( "resize orientationchange", handler );
};
WindowResizeManager.prototype.handleWindowResizeEvent = function() {
return _.debounce( this.triggerWindowResizeCompleteEvent, this.debounceWait );
};
WindowResizeManager.prototype.triggerWindowResizeCompleteEvent = function() {
this.window.trigger( "resizeCompleted.window" );
};
@chris-kobrzak
Copy link
Author

The code implicitly depends on Underscore (debounce method) and jQuery and this could certainly be improved.

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