From 9758e01971ace7be64e745682421d230bf18ca88 Mon Sep 17 00:00:00 2001 From: Rex Kelly Date: Sun, 4 Jul 2021 19:00:08 -0700 Subject: [PATCH] Added autoTriggerRateLimit, autoTriggerPauseUntilLoaded functionality and autoTriggerLastFiredAt property to jScroll along with sensible defaults. I ran across the jScroll library in the wild... I noticed that particular implementation was a bit broken, from a low bandwidth users perspective, as the page kept auto triggering before any of the images even loaded... I'm sure this is the fault of the site developers, for not styling their HTML so their images had sensible pre-defined dimensions... but I thought I would contribute a simple patch to account for that. --- jquery.jscroll.js | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/jquery.jscroll.js b/jquery.jscroll.js index 951b416..6e3575f 100644 --- a/jquery.jscroll.js +++ b/jquery.jscroll.js @@ -19,6 +19,9 @@ debug: false, autoTrigger: true, autoTriggerUntil: false, + autoTriggerRateLimit: 1000, + autoTriggerLastFiredAt: null, + autoTriggerPauseUntilLoaded: true, loadingHtml: 'Loading...', loadingFunction: false, padding: 0, @@ -144,10 +147,36 @@ // Load the next set of content, if available _load = function() { - var $inner = $e.find('div.jscroll-inner').first(), - data = $e.data('jscroll'); - data.waiting = true; + // Respect rate limits, if set... + if(_options.autoTriggerRateLimit !== false) { + + let now = new Date().valueOf(); + + if( now < _options.autoTriggerLastFiredAt + _options.autoTriggerLastFiredAt ) { + return; // Exit early and avoid preemptive overhead. + } else { + _options.autoTriggerLastFiredAt = now; + } + } + + var $inner = $e.find('div.jscroll-inner').first(); + + // Check that any images in currently enqueued elements have completed loading... + if(_options.autoTriggerPauseUntilLoaded === true ){ + let enquedImages = $inner.find('img'); + if( enquedImages.length ) { + for (var i = enquedImages.length - 1; i >= 0; i--){ + if(!enquedImages[i].complete){ + return _debug('autoTriggerAborted', '-- Still Loading Images --'); + } + } + } + } + + var data = $e.data('jscroll'); + data.waiting = true; + $inner.append('
') .children('.jscroll-added').last() .html('
' + _options.loadingHtml + '
') @@ -158,6 +187,9 @@ } }); + + if($e.find('div.jscroll-inner').first()) + return $e.animate({scrollTop: $inner.outerHeight()}, 0, function() { var nextHref = data.nextHref; $inner.find('div.jscroll-added').last().load(nextHref, function(r, status) { @@ -223,4 +255,4 @@ }); }; -})(jQuery); \ No newline at end of file +})(jQuery);