Skip to content

Instantly share code, notes, and snippets.

@scottjehl
Created June 30, 2011 20:26
Show Gist options
  • Save scottjehl/1057140 to your computer and use it in GitHub Desktop.
Save scottjehl/1057140 to your computer and use it in GitHub Desktop.
Simple throttled resize event
// throttled resize event
(function( $ ) {
$.event.special.throttledresize = {
setup: function() {
$( this ).bind( "resize", handler );
},
teardown: function(){
$( this ).unbind( "resize", handler );
}
};
var throttle = 250,
handler = function() {
curr = ( new Date() ).getTime();
diff = curr - lastCall;
if ( diff >= throttle ) {
lastCall = curr;
$( this ).trigger( "throttledresize" );
} else {
if ( heldCall ) {
clearTimeout( heldCall );
}
// Promise a held call will still execute
heldCall = setTimeout( handler, throttle - diff );
}
},
lastCall = 0,
heldCall,
curr,
diff;
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment