Skip to content

Commit 98f47ba

Browse files
committed
Merge pull request mozilla#138 from notmasteryet/issue-132-part-C
loading the max 15 thumbnails first time
2 parents ae16e8f + 32f681c commit 98f47ba

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

multi_page_viewer.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ var PDFViewer = {
277277
openURL: function(url) {
278278
PDFViewer.url = url;
279279
document.title = url;
280+
281+
if (this.thumbsLoadingInterval) {
282+
// cancel thumbs loading operations
283+
clearInterval(this.thumbsLoadingInterval);
284+
this.thumbsLoadingInterval = null;
285+
}
280286

281287
var req = new XMLHttpRequest();
282288
req.open('GET', url);
@@ -293,7 +299,9 @@ var PDFViewer = {
293299

294300
req.send(null);
295301
},
296-
302+
303+
thumbsLoadingInterval: null,
304+
297305
readPDF: function(data) {
298306
while (PDFViewer.element.hasChildNodes()) {
299307
PDFViewer.element.removeChild(PDFViewer.element.firstChild);
@@ -315,12 +323,22 @@ var PDFViewer = {
315323
PDFViewer.drawPage(1);
316324
document.location.hash = 1;
317325

318-
setTimeout(function() {
319-
for (var i = 1; i <= PDFViewer.numberOfPages; i++) {
320-
PDFViewer.createThumbnail(i);
321-
PDFViewer.drawThumbnail(i);
326+
// slowly loading the thumbs (few per second)
327+
// first time we are loading more images than subsequent
328+
var currentPageIndex = 1, imagesToLoad = 15;
329+
this.thumbsLoadingInterval = setInterval((function() {
330+
while (imagesToLoad-- > 0) {
331+
if (currentPageIndex > PDFViewer.numberOfPages) {
332+
clearInterval(this.thumbsLoadingInterval);
333+
this.thumbsLoadingInterval = null;
334+
return;
335+
}
336+
PDFViewer.createThumbnail(currentPageIndex);
337+
PDFViewer.drawThumbnail(currentPageIndex);
338+
++currentPageIndex;
322339
}
323-
}, 500);
340+
imagesToLoad = 3; // next time loading less images
341+
}).bind(this), 500);
324342
}
325343

326344
PDFViewer.previousPageButton.className = (PDFViewer.pageNumber === 1) ? 'disabled' : '';

0 commit comments

Comments
 (0)