Skip to content

Instantly share code, notes, and snippets.

@thisiskylierose
Last active January 15, 2019 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thisiskylierose/16756b9021bad5e484df to your computer and use it in GitHub Desktop.
Save thisiskylierose/16756b9021bad5e484df to your computer and use it in GitHub Desktop.
jquery.smartresize.js
(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