Debounces scroll events in web viewer.

As requested in #5178, this change debounces the scroll events.
The reason for doing so is that browsers can event-storm especially on
scroll, communicating hundreds of subpixel changes.

The main reason for this resulting in poor performance is that on each
scroll event `scrollTop` was queried, which forces layouting.

This change will use `requestAnimationFrame` to make sure the browser can
allocate enough time to other tasks. The delay is however quite small, thus
the reduction in executions is less noticeable. Modern browsers however utilize
`requestAnimationFrame` to smoothen out rendering.
This commit is contained in:
Fabian Lange 2014-08-18 21:16:46 +02:00
parent 6ac995dc88
commit 403a4e2bda

View File

@ -274,7 +274,14 @@ var PDFView = {
watchScroll: function pdfViewWatchScroll(viewAreaElement, state, callback) {
state.down = true;
state.lastY = viewAreaElement.scrollTop;
viewAreaElement.addEventListener('scroll', function webViewerScroll(evt) {
state.rAF = null;
viewAreaElement.addEventListener('scroll', function debounceScroll(evt) {
if (state.rAF) {
return;
}
// schedule an invocation of webViewerScrolled for next animation frame.
state.rAF = window.requestAnimationFrame(function webViewerScrolled() {
state.rAF = null;
if (!PDFView.pdfDocument) {
return;
}
@ -288,6 +295,7 @@ var PDFView = {
// else do nothing and use previous value
state.lastY = currentY;
callback();
});
}, true);
},