Skip to content

Instantly share code, notes, and snippets.

@tvpmb
Forked from aarongustafson/watchResize.js
Created November 28, 2012 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvpmb/4159411 to your computer and use it in GitHub Desktop.
Save tvpmb/4159411 to your computer and use it in GitHub Desktop.
Efficient callback management for window.onresize
(function(window){
window.watchResize = function(callback)
{
var resizing;
callback.size = 0;
function done()
{
var curr_size = window.innerWidth;
clearTimeout( resizing );
resizing = null;
// only run on a true resize
if ( callback.size != curr_size )
{
callback();
callback.size = curr_size;
}
}
window.addEventListener('resize', function(){
if ( resizing )
{
clearTimeout( resizing );
resizing = null;
}
resizing = setTimeout( done, 50 );
});
// init
callback();
};
}(window));
(function(a){a.watchResize=function(d){var c;d.size=0;function b(){var e=a.innerWidth;clearTimeout(c);c=null;if(d.size!=e){d();d.size=e}}a.addEventListener("resize",function(){if(c){clearTimeout(c);c=null}c=setTimeout(b,50)});d()}}(window));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment