Skip to content

Instantly share code, notes, and snippets.

@quis
Created March 18, 2012 11:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quis/2070586 to your computer and use it in GitHub Desktop.
Save quis/2070586 to your computer and use it in GitHub Desktop.
jQuery-based window resizing hook with rate limiting
/*
Usage: $.windowResize(yourFunction);
Intended as a replacement for $(window).resize(yourFunction)
yourFunction will only be called once the resize action has been completed, improving performance
Code is public domain
*/
jQuery.windowResize = (function($) {
var enqueue = function() { // Setter method for the queue of callbacks
var callbacks = Array.prototype.slice.call(arguments);
for (var i in callbacks) queue.push(callbacks[i]);
},
flush = function() { // Check every 100ms to see if resize has finished
clearTimeout(timer);
timer = setTimeout(doCallbacks, 100);
},
doCallbacks = function() { // Run any functions that have been queued
for (var i in queue) queue[i].call(window);
},
timer,
queue = [];
$(function() {
$(window)
.resize(flush); // Bind to jQuery window object just once
});
return enqueue;
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment