Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @function
- * @name debounce
- * @description Utility method for debouncing the resize event
- * @param {function} func
- * @param {number} wait
- * @param {object} immediate
- * @example var myEfficientFn = debounce(function() { things to do }, 250);
- * @example window.addEventListener( 'resize', myEfficientFn );
- */
- var debounce = function ( func, wait, immediate ) {
- var timeout;
- return function() {
- var context = this;
- var args = arguments;
- var later = function() {
- timeout = null;
- if ( ! immediate ) {
- func.apply( context, args );
- }
- };
- var callNow = immediate && !timeout;
- window.clearTimeout( timeout );
- timeout = window.setTimeout( later, wait );
- if ( callNow ) {
- func.apply( context, args );
- }
- };
- };
Add Comment
Please, Sign In to add comment