Skip to content

Commit

Permalink
Prevent picturefill from running over the same image multiple times w…
Browse files Browse the repository at this point in the history
…hen loading async via polling
  • Loading branch information
jansepar committed Mar 26, 2014
1 parent a334428 commit e56f713
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/picturefill.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,22 @@
return formattedCandidates;
};

w.picturefill = function() {
w.picturefill = function(checkEvaluated) {
// Loop through all images on the page that are `<picture>`
var pictures = doc.getElementsByTagName("picture");
for (var i=0, plen = pictures.length; i < plen; i++) {
var picture = pictures[i];

// if checkEvaluated is true, we will not re-run on picture elements
// that have already been evaluated. This is a useful mode to have
// when loading the picturefill async and polling the document
// as more of it downloads to fetch images as soon as possible.
if (checkEvaluated) {
if (picture.hasAttribute('evaluated')) {
continue;
};
picture.setAttribute('evaluated', true);
}
var matches = [];

// In IE9, <source> elements get removed if they aren't children of
Expand Down Expand Up @@ -246,13 +257,15 @@
var intervalId = setInterval(function(){
// When the document has finished loading, stop checking for new images
// https://github.com/ded/domready/blob/master/ready.js#L15
w.picturefill();
w.picturefill(true);
if (/^loaded|^i|^c/.test(doc.readyState)) {
clearInterval(intervalId);
return;
}
}, 250);
w.addEventListener("resize", w.picturefill, false);
w.addEventListener("resize", function() {
w.picturefill();
}, false);
};
runPicturefill();

Expand Down

0 comments on commit e56f713

Please sign in to comment.