diff --git a/src/openseadragon.js b/src/openseadragon.js index d3ea55e0..be45ac07 100644 --- a/src/openseadragon.js +++ b/src/openseadragon.js @@ -2338,41 +2338,10 @@ function OpenSeadragon( options ){ return $.createAjaxRequest( local ); }, - queueAjaxRequest: function ( - request, - method, - url, - headers, - withCredentials, - responseType, - postData - ) { + queueAjaxRequest: function (request, sendRequestFunc) { var oldOnStateChange = request.onreadystatechange; - var sendRequest = function () { - request.open( method, url, true ); - - if (responseType) { - request.responseType = responseType; - } - - if (headers) { - for (var headerName in headers) { - if (Object.prototype.hasOwnProperty.call(headers, headerName) && headers[headerName]) { - request.setRequestHeader(headerName, headers[headerName]); - } - } - } - - if (withCredentials) { - request.withCredentials = true; - } - - request.send(postData); - }; - - var callback = function() { - + var onCompleteRequest = function() { $.ajaxQueue.numRequests--; if ( @@ -2387,7 +2356,7 @@ function OpenSeadragon( options ){ request.onreadystatechange = function() { if ( request.readyState === 4 ) { - callback(); + onCompleteRequest(); oldOnStateChange(); } }; @@ -2395,10 +2364,10 @@ function OpenSeadragon( options ){ $.ajaxQueue.numRequests++; if ($.ajaxQueue.numActiveRequests === $.ajaxQueue.maxConcurrency) { - $.ajaxQueue.requestFuncs.push(sendRequest); + $.ajaxQueue.requestFuncs.push(sendRequestFunc); } else { $.ajaxQueue.numActiveRequests++; - sendRequest(); + sendRequestFunc(); } }, @@ -2464,16 +2433,31 @@ function OpenSeadragon( options ){ }; var method = postData ? "POST" : "GET"; + + var sendRequest = function () { + request.open( method, url, true ); + + if (responseType) { + request.responseType = responseType; + } + + if (headers) { + for (var headerName in headers) { + if (Object.prototype.hasOwnProperty.call(headers, headerName) && headers[headerName]) { + request.setRequestHeader(headerName, headers[headerName]); + } + } + } + + if (withCredentials) { + request.withCredentials = true; + } + + request.send(postData); + }; + try { - $.queueAjaxRequest( - request, - method, - url, - headers, - withCredentials, - responseType, - postData - ); + $.queueAjaxRequest(request, sendRequest); } catch (e) { $.console.error( "%s while making AJAX request: %s", e.name, e.message );