Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix weird callstack bug

FYI it was triggered on every browsers by the onload= being called at start AND when we switched from data-src -> src
On IE it was trggering the unfamous stack overflow at line 0 bug
Now also a LOT faster motherfucker
  • Loading branch information...
commit d1bddf3207f9b873df65d7e46de9eceeabc7690e 1 parent c520c29
vvo authored
Showing with 28 additions and 30 deletions.
  1. +25 −27 LL.js
  2. +2 −2 LL.min.js
  3. +1 −1  package.json
View
52 LL.js
@@ -14,7 +14,7 @@
}
}
- // cross browser event handling
+ // cross browser event add
function addEvent( el, type, fn ) {
if (el.attachEvent) {
el.attachEvent && el.attachEvent( 'on' + type, fn );
@@ -23,7 +23,7 @@
}
}
- // cross browser event handling
+ // cross browser event remove
function removeEvent( el, type, fn ) {
if (el.detachEvent) {
el.detachEvent && el.detachEvent( 'on' + type, fn );
@@ -32,10 +32,10 @@
}
}
- // glocal variables
var
lazyAttr = 'data-src',
winH,
+ imgs = [],
pageHasLoaded,
// cross browser window height
@@ -44,19 +44,19 @@
(document.documentElement && document.documentElement.clientHeight) ||
(document.body && document.body.clientHeight) ||
10000;
- }, 77),
+ }, 50),
checkImagesThrottle = throttle(function checkImages() {
var
- imgs = document.getElementsByTagName('img'),
last = imgs.length,
current,
allImagesDone = true;
for (current = 0; current < last; current++) {
+ var img = imgs[current];
// if showIfVisible is false, it means we have some waiting images to be
// shown
- if(showIfVisible(imgs[current]) === false) {
+ if(img !== undefined && showIfVisible(img, current) === false) {
allImagesDone = false;
}
}
@@ -64,18 +64,14 @@
if (allImagesDone && pageHasLoaded) {
unsubscribe();
}
- }, 77);
-
- function showIfVisible(el) {
- if (el.getAttribute(lazyAttr) === null) {
- // img already shown
- return true;
- }
+ }, 5);
+ function showIfVisible(img, index) {
// 200 is the vertical offset used for preloading soon to be visible images
- if (el.getBoundingClientRect().top < winH + 200) {
- el.src = el.getAttribute(lazyAttr);
- el.removeAttribute(lazyAttr);
+ if (img.getBoundingClientRect().top < winH + 200) {
+ img.src = img.getAttribute(lazyAttr);
+ img.removeAttribute(lazyAttr);
+ imgs.splice(index, 1);
// img shown
return true;
} else {
@@ -89,6 +85,19 @@
removeEvent(window, 'scroll', checkImagesThrottle);
}
+ // Indicates that the page is optimized
+ window['fstrz'] = true;
+ // Import any fzns object (Fasterize Name Space)
+ window['fzns'] = window['fzns'] || {};
+ // Export and creates LL.s() to be used by <img onload=/>
+ window['fzns']['LL'] = {
+ 's': function(el) {
+ // To avoid onload being called and called and called ...
+ el.onload = null;
@vvo Owner
vvo added a note

This is where mod_pagespeed lazyload fails

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ showIfVisible(el, imgs.push(el) - 1);
+ }
+ };
+
getWindowHeightThrottle();
addEvent(window, 'resize', getWindowHeightThrottle);
addEvent(window, 'scroll', checkImagesThrottle);
@@ -96,15 +105,4 @@
pageHasLoaded = true;
});
- // Indicates that the page is optimized
- window['fstrz'] = true;
-
- // Import any fzns object
- window['fzns'] = window['fzns'] || {};
-
- // Export and creates LL.show() to be used by <img onload=/>
- window['fzns']['LL'] = {
- 's': showIfVisible
- };
-
})(this, document)
View
4 LL.min.js
@@ -1,2 +1,2 @@
-(function(b,e){function h(a){var b=0;return function(){var c=+new Date;77>c-b||(b=c,a.apply(this,arguments))}}function f(a,b,c){a.attachEvent?a.attachEvent&&a.attachEvent("on"+b,c):a.addEventListener(b,c,!1)}function i(a,b,c){a.detachEvent?a.detachEvent&&a.detachEvent("on"+b,c):a.removeEventListener(b,c,!1)}function j(a){if(null===a.getAttribute(d))return!0;return a.getBoundingClientRect().top<k+200?(a.src=a.getAttribute(d),a.removeAttribute(d),!0):!1}var d="data-src",k,l,g=h(function(){k=b.innerHeight||
-e.documentElement&&e.documentElement.clientHeight||e.body&&e.body.clientHeight||1E4}),m=h(function(){var a=e.getElementsByTagName("img"),f=a.length,c,d=!0;for(c=0;c<f;c++)!1===j(a[c])&&(d=!1);d&&l&&(i(b,"resize",g),i(b,"scroll",m))});g();f(b,"resize",g);f(b,"scroll",m);f(b,"load",function(){l=!0});b.fstrz=!0;b.fzns=b.fzns||{};b.fzns.LL={s:j}})(this,document);
+(function(b,e){function i(a,c){var d=0;return function(){var b=+new Date;b-d<c||(d=b,a.apply(this,arguments))}}function g(a,c,b){a.attachEvent?a.attachEvent&&a.attachEvent("on"+c,b):a.addEventListener(c,b,!1)}function j(a,b,d){a.detachEvent?a.detachEvent&&a.detachEvent("on"+b,d):a.removeEventListener(b,d,!1)}function k(a,b){return a.getBoundingClientRect().top<l+200?(a.src=a.getAttribute(m),a.removeAttribute(m),f.splice(b,1),!0):!1}var m="data-src",l,f=[],n,h=i(function(){l=b.innerHeight||e.documentElement&&
+e.documentElement.clientHeight||e.body&&e.body.clientHeight||1E4},50),o=i(function(){var a=f.length,c,d=!0;for(c=0;c<a;c++){var e=f[c];void 0!==e&&!1===k(e,c)&&(d=!1)}d&&n&&(j(b,"resize",h),j(b,"scroll",o))},5);b.fstrz=!0;b.fzns=b.fzns||{};b.fzns.LL={s:function(a){a.onload=null;k(a,f.push(a)-1)}};h();g(b,"resize",h);g(b,"scroll",o);g(b,"load",function(){n=!0})})(this,document);
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "LL",
"description": "Lazy load my friend",
- "version": "0.4.0",
+ "version": "0.5.0",
"repository": {
"type": "git",
"url": "http://github.com/fasterize/LL.git"
Please sign in to comment.
Something went wrong with that request. Please try again.