From c917f07606ea1ba8ad1d0078ee27c57927391474 Mon Sep 17 00:00:00 2001 From: christianesperar Date: Thu, 7 Jul 2016 11:08:54 +0800 Subject: [PATCH] Missing smartresize helper --- src/js/helpers/smartresize.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/js/helpers/smartresize.js diff --git a/src/js/helpers/smartresize.js b/src/js/helpers/smartresize.js new file mode 100644 index 0000000..cd6824b --- /dev/null +++ b/src/js/helpers/smartresize.js @@ -0,0 +1,35 @@ +/** + * Resize function without multiple trigger + * + * Usage: + * $(window).smartresize(function(){ + * // code here + * }); + */ +(function($,sr){ + // debouncing function from John Hann + // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/ + var debounce = function (func, threshold, execAsap) { + var timeout; + + return function debounced () { + var obj = this, args = arguments; + function delayed () { + if (!execAsap) + func.apply(obj, args); + timeout = null; + } + + if (timeout) + clearTimeout(timeout); + else if (execAsap) + func.apply(obj, args); + + timeout = setTimeout(delayed, threshold || 100); + }; + }; + + // smartresize + jQuery.fn[sr] = function(fn){ return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); }; + +})(jQuery,'smartresize'); \ No newline at end of file