Created
August 28, 2013 18:14
-
-
Save dbushell/6369330 to your computer and use it in GitHub Desktop.
This file contains 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
// 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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can be done without jQuery of course!