From 8cb2714daea991c78823afc1bb1899e22f36c2b8 Mon Sep 17 00:00:00 2001 From: Benjamin Gilbert Date: Fri, 26 Apr 2013 22:32:51 -0400 Subject: [PATCH] Fix timer leak after multiple Viewer.open() calls, take 2 Track the request ID of the outstanding animation frame and cancel it on Viewer.close(). Fixes #76. --- src/viewer.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/viewer.js b/src/viewer.js index f0b698b8..af4b1822 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -115,6 +115,7 @@ $.Viewer = function( options ) { THIS[ this.hash ] = { "fsBoundsDelta": new $.Point( 1, 1 ), "prevContainerSize": null, + "updateRequestId": null, "animating": false, "forceRedraw": false, "mouseInside": false, @@ -353,7 +354,11 @@ $.extend( $.Viewer.prototype, $.EventHandler.prototype, $.ControlDock.prototype, * @return {OpenSeadragon.Viewer} Chainable. */ close: function ( ) { - + if ( THIS[ this.hash ].updateRequestId !== null ){ + $.cancelAnimationFrame( THIS[ this.hash ].updateRequestId ); + THIS[ this.hash ].updateRequestId = null; + } + if( this.drawer ){ this.drawer.clearOverlays(); } @@ -1064,7 +1069,7 @@ function openTileSource( viewer, source ) { THIS[ _this.hash ].animating = false; THIS[ _this.hash ].forceRedraw = true; - scheduleUpdate( _this, updateMulti ); + THIS[ _this.hash ].updateRequestId = scheduleUpdate( _this, updateMulti ); //Assuming you had programatically created a bunch of overlays //and added them via configuration @@ -1338,13 +1343,11 @@ function updateMulti( viewer ) { var beginTime; - if ( !viewer.source ) { - return; - } - beginTime = +new Date(); updateOnce( viewer ); - scheduleUpdate( viewer, arguments.callee, beginTime ); + + THIS[ viewer.hash ].updateRequestId = scheduleUpdate( viewer, + arguments.callee, beginTime ); } function updateOnce( viewer ) {