Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// http://jasonwyatt.tumblr.com/post/10481498815/how-to-correctly-debounce-a-javascript-function
function debounce(fn, debounceDuration)
{
debounceDuration = debounceDuration || 100;
return function(){
if(!fn.debouncing){
var args = Array.prototype.slice.apply(arguments);
fn.lastReturnVal = fn.apply(window, args);
fn.debouncing = true;
}
clearTimeout(fn.debounceTimeout);
fn.debounceTimeout = setTimeout(function(){
fn.debouncing = false;
}, debounceDuration);
return fn.lastReturnVal;
};
}
var win = $(window),
win_width,
win_height,
resize_queue = [];
resize_queue.push(function()
{
win_width = win.width();
win_height = win.height();
// do stuff ...
});
var doResize = function()
{
for (var i = 0; i < resize_queue.length; i++) {
if (typeof resize_queue[i] === 'function') {
resize_queue[i]();
}
}
};
win.on('resize orientationchange', debounce(function(e) { doResize(); }, 50));
@dbushell

This comment has been minimized.

Copy link
Owner Author

@dbushell dbushell commented Aug 28, 2013

can be done without jQuery of course!

@dbushell

This comment has been minimized.

Copy link
Owner Author

@dbushell dbushell commented Aug 28, 2013

Or just CSS with height: 100vh; but be wary of the disastrous iOS bug...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.