From 0112f6a4ea0b156e2644da5965e3a65aed7747e0 Mon Sep 17 00:00:00 2001 From: Ian Gilman Date: Mon, 17 Jun 2013 11:28:42 -0700 Subject: [PATCH] Improved requestAnimationFrame polyfill Processes all outstanding frame requests per frame, rather than one at a time. --- src/openseadragon.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/openseadragon.js b/src/openseadragon.js index c00673c8..5531831b 100644 --- a/src/openseadragon.js +++ b/src/openseadragon.js @@ -1675,6 +1675,7 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){ }; } else { var aAnimQueue = [], + processing = [], iRequestId = 0, iIntervalId; @@ -1685,7 +1686,15 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){ if ( !iIntervalId ) { iIntervalId = setInterval( function() { if ( aAnimQueue.length ) { - aAnimQueue.shift( )[ 1 ](+new Date()); + // Process all of the currently outstanding frame + // requests, but none that get added during the + // processing. + var time = +new Date(); + processing = aAnimQueue; + aAnimQueue = []; + while ( processing.length ) { + processing.shift()[ 1 ]( time ); + } } else { // don't continue the interval, if unnecessary clearInterval( iIntervalId ); @@ -1700,12 +1709,22 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){ // create a mock cancelAnimationFrame function $.cancelAnimationFrame = function( requestId ) { // find the request ID and remove it - for ( var i = 0, j = aAnimQueue.length; i < j; i += 1 ) { + var i, j; + for ( i = 0, j = aAnimQueue.length; i < j; i += 1 ) { if ( aAnimQueue[ i ][ 0 ] === requestId ) { aAnimQueue.splice( i, 1 ); return; } } + + // If it's not in the queue, it may be in the set we're currently + // processing. + for ( i = 0, j = processing.length; i < j; i += 1 ) { + if ( processing[ i ][ 0 ] === requestId ) { + processing.splice( i, 1 ); + return; + } + } }; } })( window );