diff options
author | Jed Foster <jed@jedfoster.com> | 2014-12-31 11:47:38 -0800 |
---|---|---|
committer | Jed Foster <jed@jedfoster.com> | 2014-12-31 15:50:51 -0800 |
commit | 4cdf9a4ba6f5b41361072a436e77add54c5e39f6 (patch) | |
tree | f154f5b135b36686f23515fde987cec6ee5e00c6 | |
parent | 3da0aef83fc823e8008c4a08d8630eea637081e0 (diff) | |
download | Readmore.js-4cdf9a4ba6f5b41361072a436e77add54c5e39f6.zip Readmore.js-4cdf9a4ba6f5b41361072a436e77add54c5e39f6.tar.gz Readmore.js-4cdf9a4ba6f5b41361072a436e77add54c5e39f6.tar.bz2 |
Debounce resizeBoxes method
Closes #37 and #55
-rw-r--r-- | readmore.js | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/readmore.js b/readmore.js index 15803a2..549a576 100644 --- a/readmore.js +++ b/readmore.js @@ -3,6 +3,8 @@ * Author: @jed_foster * Project home: jedfoster.github.io/Readmore.js * Licensed under the MIT license + * + * Debounce function from http://davidwalsh.name/javascript-debounce-function */ ;(function($) { @@ -26,6 +28,21 @@ }, cssEmbedded = {}; + 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; + clearTimeout(timeout); + timeout = setTimeout(later, wait); + if (callNow) func.apply(context, args); + }; + } + function Readmore( element, options ) { this.element = element; @@ -103,7 +120,7 @@ } }); - $(window).on('resize', function(event) { + window.addEventListener('resize', function(event) { $this.resizeBoxes(); }); }, @@ -154,7 +171,7 @@ element.data('expandedHeight', height); }, - resizeBoxes: function() { + resizeBoxes: debounce(function() { var $this = this; $('.readmore-js-section').each(function() { @@ -166,7 +183,7 @@ current.css('height', current.data('expandedHeight')); } }); - }, + }, 100), destroy: function() { var $this = this; |