Created
March 18, 2012 11:09
-
-
Save quis/2070586 to your computer and use it in GitHub Desktop.
jQuery-based window resizing hook with rate limiting
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
/* | |
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