Skip to content

Commit

Permalink
added timeout option requestIdleCallback (setting ricTimeout option b…
Browse files Browse the repository at this point in the history
…elow 150 fixes #434)
  • Loading branch information
alexander.farkas committed Oct 14, 2017
1 parent 27a1386 commit 90b5f2b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 48 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ 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.
```html
<script>
window.lazySizesConfig = window.lazySizesConfig || {};
Expand Down
97 changes: 49 additions & 48 deletions src/lazysizes-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,29 +158,30 @@ function l(window, document) {
var running;
var lastTime = 0;
var gDelay = 125;
var RIC_DEFAULT_TIMEOUT = 666;
var rICTimeout = RIC_DEFAULT_TIMEOUT;
var rICTimeout = lazySizesConfig.ricTimeout;
var run = function(){
running = false;
lastTime = Date.now();
fn();
};
var idleCallback = requestIdleCallback ?
var idleCallback = requestIdleCallback && lazySizesConfig.ricTimeout ?
function(){
requestIdleCallback(run, {timeout: rICTimeout});
if(rICTimeout !== RIC_DEFAULT_TIMEOUT){
rICTimeout = RIC_DEFAULT_TIMEOUT;

if(rICTimeout !== lazySizesConfig.ricTimeout){
rICTimeout = lazySizesConfig.ricTimeout;
}
}:
} :
rAFIt(function(){
setTimeout(run);
}, true)
;

return function(isPriority){
var delay;

if((isPriority = isPriority === true)){
rICTimeout = 44;
rICTimeout = 33;
}

if(running){
Expand Down Expand Up @@ -230,6 +231,47 @@ function l(window, document) {
};
};

(function(){
var prop;

var lazySizesDefaults = {
lazyClass: 'lazyload',
loadedClass: 'lazyloaded',
loadingClass: 'lazyloading',
preloadClass: 'lazypreload',
errorClass: 'lazyerror',
//strictClass: 'lazystrict',
autosizesClass: 'lazyautosizes',
srcAttr: 'data-src',
srcsetAttr: 'data-srcset',
sizesAttr: 'data-sizes',
//preloadAfterLoad: false,
minSize: 40,
customMedia: {},
init: true,
expFactor: 1.5,
hFac: 0.8,
loadMode: 2,
loadHidden: true,
ricTimeout: 300,
};

lazySizesConfig = window.lazySizesConfig || window.lazysizesConfig || {};

for(prop in lazySizesDefaults){
if(!(prop in lazySizesConfig)){
lazySizesConfig[prop] = lazySizesDefaults[prop];
}
}

window.lazySizesConfig = lazySizesConfig;

setTimeout(function(){
if(lazySizesConfig.init){
init();
}
});
})();

var loader = (function(){
var preloadElems, isCompleted, resetPreloadingTimer, loadMode, started;
Expand Down Expand Up @@ -630,47 +672,6 @@ function l(window, document) {
}
};

(function(){
var prop;

var lazySizesDefaults = {
lazyClass: 'lazyload',
loadedClass: 'lazyloaded',
loadingClass: 'lazyloading',
preloadClass: 'lazypreload',
errorClass: 'lazyerror',
//strictClass: 'lazystrict',
autosizesClass: 'lazyautosizes',
srcAttr: 'data-src',
srcsetAttr: 'data-srcset',
sizesAttr: 'data-sizes',
//preloadAfterLoad: false,
minSize: 40,
customMedia: {},
init: true,
expFactor: 1.5,
hFac: 0.8,
loadMode: 2,
loadHidden: true,
};

lazySizesConfig = window.lazySizesConfig || window.lazysizesConfig || {};

for(prop in lazySizesDefaults){
if(!(prop in lazySizesConfig)){
lazySizesConfig[prop] = lazySizesDefaults[prop];
}
}

window.lazySizesConfig = lazySizesConfig;

setTimeout(function(){
if(lazySizesConfig.init){
init();
}
});
})();

lazysizes = {
cfg: lazySizesConfig,
autoSizer: autoSizer,
Expand Down

0 comments on commit 90b5f2b

Please sign in to comment.