Skip to content

Instantly share code, notes, and snippets.

@dbushell
Created August 28, 2013 18:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dbushell/6369330 to your computer and use it in GitHub Desktop.
Save dbushell/6369330 to your computer and use it in GitHub Desktop.
// 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
Copy link
Author

can be done without jQuery of course!

@dbushell
Copy link
Author

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