Last active
January 15, 2019 23:42
-
-
Save thisiskylierose/16756b9021bad5e484df to your computer and use it in GitHub Desktop.
jquery.smartresize.js
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
(function($, functionName) { | |
'use strict'; | |
// debouncing function from John Hann | |
// http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/ | |
var debounce = function(cb, threshold, execAsap) { | |
var timeout; | |
return function debounced() { | |
var obj = this, | |
args = arguments; | |
function delayed() { | |
if (!execAsap) { | |
cb.apply(obj, args); | |
} | |
timeout = null; | |
} | |
if (timeout) { | |
clearTimeout(timeout); | |
} else if (execAsap) { | |
cb.apply(obj, args); | |
} | |
timeout = setTimeout(delayed, threshold || 10); | |
}; | |
}; | |
// smartresize | |
jQuery.fn[functionName] = function(callback) { | |
return callback ? this.bind('resize', debounce(callback)) : this.trigger( | |
functionName | |
); | |
}; | |
})(jQuery, 'smartresize'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment