Created
September 28, 2016 12:24
Register resize function - an easy way to register function for debounced execution with parameters
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
/** | |
* | |
* Usage example: | |
* | |
* $('#headerghost').css('height', $('header').outerHeight()-32); | |
* | |
* registerResizeFunction(function($header, $headerghost){ | |
* $headerghost.css('height', $header.outerHeight()-32); | |
* }, $('header'), $('#headerghost')); | |
* | |
* | |
**/ | |
(function(w, $){ | |
function Debounce(func, wait, immediate) { | |
var timeout; | |
return function() { | |
var context = this, args = arguments; | |
var later = function() { | |
timeout = null; | |
if (!immediate) func.apply(context, args); | |
}; | |
var callNow = immediate && !timeout; | |
w.clearTimeout(timeout); | |
timeout = w.setTimeout(later, wait); | |
if (callNow) func.apply(context, args); | |
}; | |
}; | |
var registeredResizeFunctions=[]; | |
function registerResizeFunction(){ | |
var fn = arguments[0], | |
args = []; | |
for(var i=0;i<arguments.length;i++){ | |
if(i===0)continue; | |
args.push( arguments[i] ); | |
} | |
registeredResizeFunctions.push({ | |
"cb" : fn, | |
"args" : args | |
}); | |
} | |
function executeRegisteredResizeFunctions(){ | |
for(var i=0; i<registeredResizeFunctions.length;i++){ | |
registeredResizeFunctions[i].cb.apply(this, registeredResizeFunctions[i].args ); | |
} | |
} | |
$(w).on('resize', Debounce(executeRegisteredResizeFunctions,40)); | |
})(window, window.jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment