Skip to content

Commit

Permalink
add different throttle options
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander.farkas committed Mar 11, 2018
1 parent 150c3e5 commit 7d74351
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ Here the list of options:
* ``lazySizesConfig.sizesAttr`` (default: ``"data-sizes"``): The attribute, which should be transformed to ``sizes``. Makes almost only makes sense with the value ``"auto"``. Otherwise the ``sizes`` attribute should be used directly.
* ``lazySizesConfig.customMedia`` (default: ``{}``): The ``customMedia`` option object is an alias map for different media queries. It can be used to separate/centralize your multiple specific media queries implementation (layout) from the ``source[media]`` attribute (content/structure) by creating labeled media queries. (See also the [custommedia extension](plugins/custommedia)).
* ``lazySizesConfig.loadHidden`` (default: ``true``): Wether to load `visibility: hidden` elements.
* ``lazySizesConfig.ricTimeout`` (default: ``300``): The timeout option used for the `requestIdleCallback`. Reasonable values between: 50 - 1000.
* ``lazySizesConfig.ricTimeout`` (default: ``100``): The timeout option used for the `requestIdleCallback`. Reasonable values between: 50 - 1000.
* ``lazySizesConfig.throttleDelay`` (default: ``125``): The timeout option used to throttle all listeners. Reasonable values between: 66 - 200.
```html
<script>
window.lazySizesConfig = window.lazySizesConfig || {};
Expand Down
9 changes: 5 additions & 4 deletions lazysizes-umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,14 @@
var throttle = function(fn){
var running;
var lastTime = 0;
var gDelay = 125;
var gDelay = lazySizesConfig.throttleDelay;
var rICTimeout = lazySizesConfig.ricTimeout;
var run = function(){
running = false;
lastTime = Date.now();
fn();
};
var idleCallback = requestIdleCallback && lazySizesConfig.ricTimeout ?
var idleCallback = requestIdleCallback && rICTimeout > 49 ?
function(){
requestIdleCallback(run, {timeout: rICTimeout});

Expand Down Expand Up @@ -205,7 +205,7 @@
delay = 0;
}

if(isPriority || (delay < 9 && requestIdleCallback)){
if(isPriority || delay < 9){
idleCallback();
} else {
setTimeout(idleCallback, delay);
Expand Down Expand Up @@ -262,7 +262,8 @@
hFac: 0.8,
loadMode: 2,
loadHidden: true,
ricTimeout: 300,
ricTimeout: 100,
throttleDelay: 125,
};

lazySizesConfig = window.lazySizesConfig || window.lazysizesConfig || {};
Expand Down
2 changes: 1 addition & 1 deletion lazysizes-umd.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions lazysizes.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@
var throttle = function(fn){
var running;
var lastTime = 0;
var gDelay = 125;
var gDelay = lazySizesConfig.throttleDelay;
var rICTimeout = lazySizesConfig.ricTimeout;
var run = function(){
running = false;
lastTime = Date.now();
fn();
};
var idleCallback = requestIdleCallback && lazySizesConfig.ricTimeout ?
var idleCallback = requestIdleCallback && rICTimeout > 49 ?
function(){
requestIdleCallback(run, {timeout: rICTimeout});

Expand Down Expand Up @@ -202,7 +202,7 @@
delay = 0;
}

if(isPriority || (delay < 9 && requestIdleCallback)){
if(isPriority || delay < 9){
idleCallback();
} else {
setTimeout(idleCallback, delay);
Expand Down Expand Up @@ -259,7 +259,8 @@
hFac: 0.8,
loadMode: 2,
loadHidden: true,
ricTimeout: 300,
ricTimeout: 100,
throttleDelay: 125,
};

lazySizesConfig = window.lazySizesConfig || window.lazysizesConfig || {};
Expand Down
Loading

0 comments on commit 7d74351

Please sign in to comment.