From 32f681cfcf37e0a326a8aea69ae21596f561536f Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Wed, 29 Jun 2011 23:18:27 -0500 Subject: [PATCH] loading the max 15 thumbnails first time --- multi_page_viewer.js | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/multi_page_viewer.js b/multi_page_viewer.js index 87e2c8f14..f92f450dc 100644 --- a/multi_page_viewer.js +++ b/multi_page_viewer.js @@ -274,6 +274,12 @@ var PDFViewer = { openURL: function(url) { PDFViewer.url = url; document.title = url; + + if (this.thumbsLoadingInterval) { + // cancel thumbs loading operations + clearInterval(this.thumbsLoadingInterval); + this.thumbsLoadingInterval = null; + } var req = new XMLHttpRequest(); req.open('GET', url); @@ -290,7 +296,9 @@ var PDFViewer = { req.send(null); }, - + + thumbsLoadingInterval: null, + readPDF: function(data) { while (PDFViewer.element.hasChildNodes()) { PDFViewer.element.removeChild(PDFViewer.element.firstChild); @@ -312,12 +320,22 @@ var PDFViewer = { PDFViewer.drawPage(1); document.location.hash = 1; - setTimeout(function() { - for (var i = 1; i <= PDFViewer.numberOfPages; i++) { - PDFViewer.createThumbnail(i); - PDFViewer.drawThumbnail(i); + // slowly loading the thumbs (few per second) + // first time we are loading more images than subsequent + var currentPageIndex = 1, imagesToLoad = 15; + this.thumbsLoadingInterval = setInterval((function() { + while (imagesToLoad-- > 0) { + if (currentPageIndex > PDFViewer.numberOfPages) { + clearInterval(this.thumbsLoadingInterval); + this.thumbsLoadingInterval = null; + return; + } + PDFViewer.createThumbnail(currentPageIndex); + PDFViewer.drawThumbnail(currentPageIndex); + ++currentPageIndex; } - }, 500); + imagesToLoad = 3; // next time loading less images + }).bind(this), 500); } PDFViewer.previousPageButton.className = (PDFViewer.pageNumber === 1) ? 'disabled' : '';