Skip to content

Commit a66c175

Browse files
author
pandamicro
committed
Made xhr ontimeout callback work on all browsers
1 parent 1fe74d3 commit a66c175

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

CCBoot.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,9 @@ cc.loader = (function () {
623623
getXMLHttpRequest: function () {
624624
var xhr = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP");
625625
xhr.timeout = 10000;
626+
if (xhr.ontimeout === undefined) {
627+
xhr._timeoutId = -1;
628+
}
626629
return xhr;
627630
},
628631

@@ -770,12 +773,24 @@ cc.loader = (function () {
770773
} else {
771774
if (xhr.overrideMimeType) xhr.overrideMimeType("text\/plain; charset=utf-8");
772775
xhr.onload = function () {
773-
if (xhr.readyState === 4)
776+
if (xhr._timeoutId >= 0) {
777+
clearTimeout(xhr._timeoutId);
778+
}
779+
if (xhr.readyState === 4) {
774780
xhr.status === 200 ? cb(null, xhr.responseText) : cb({status:xhr.status, errorMessage:errInfo}, null);
781+
}
775782
};
776-
xhr.onerror = xhr.ontimeout = function () {
783+
xhr.onerror = function () {
777784
cb({status: xhr.status, errorMessage: errInfo}, null);
778785
};
786+
if (xhr.ontimeout === undefined) {
787+
xhr._timeoutId = setTimeout(function () {
788+
xhr.ontimeout();
789+
}, xhr.timeout);
790+
}
791+
xhr.ontimeout = function () {
792+
cb({status: xhr.status, errorMessage: "Request timeout: " + errInfo}, null);
793+
};
779794
}
780795
xhr.send(null);
781796
} else {
@@ -787,22 +802,34 @@ cc.loader = (function () {
787802
},
788803

789804
loadCsb: function(url, cb){
790-
var xhr = new XMLHttpRequest(),
805+
var xhr = cc.loader.getXMLHttpRequest(),
791806
errInfo = "load " + url + " failed!";
792807
xhr.open("GET", url, true);
793808
xhr.responseType = "arraybuffer";
794809

795810
xhr.onload = function () {
811+
if (xhr._timeoutId >= 0) {
812+
clearTimeout(xhr._timeoutId);
813+
}
796814
var arrayBuffer = xhr.response; // Note: not oReq.responseText
797815
if (arrayBuffer) {
798816
window.msg = arrayBuffer;
799817
}
800-
if(xhr.readyState === 4)
818+
if (xhr.readyState === 4) {
801819
xhr.status === 200 ? cb(null, xhr.response) : cb({status:xhr.status, errorMessage:errInfo}, null);
820+
}
802821
};
803-
xhr.onerror = xhr.ontimeout = function(){
822+
xhr.onerror = function(){
804823
cb({status:xhr.status, errorMessage:errInfo}, null);
805824
};
825+
if (xhr.ontimeout === undefined) {
826+
xhr._timeoutId = setTimeout(function () {
827+
xhr.ontimeout();
828+
}, xhr.timeout);
829+
}
830+
xhr.ontimeout = function () {
831+
cb({status: xhr.status, errorMessage: "Request timeout: " + errInfo}, null);
832+
};
806833
xhr.send(null);
807834
},
808835

@@ -2650,8 +2677,7 @@ cc.game = /** @lends cc.game# */{
26502677
this._renderContext = cc._renderContext = cc.webglContext
26512678
= cc.create3DContext(localCanvas, {
26522679
'stencil': true,
2653-
'alpha': false,
2654-
'preserveDrawingBuffer': false
2680+
'alpha': false
26552681
});
26562682
}
26572683
// WebGL context created successfully

cocos2d/audio/CCAudio.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,15 @@ cc.Audio.WebAudio.prototype = {
359359
loadBuffer: function (url, cb) {
360360
if (!SWA) return; // WebAudio Buffer
361361

362-
var request = new XMLHttpRequest();
362+
var request = cc.loader.getXMLHttpRequest();
363363
request.open("GET", url, true);
364364
request.responseType = "arraybuffer";
365365

366366
// Our asynchronous callback
367367
request.onload = function () {
368+
if (request._timeoutId >= 0) {
369+
clearTimeout(request._timeoutId);
370+
}
368371
context["decodeAudioData"](request.response, function (buffer) {
369372
//success
370373
cb(null, buffer);
@@ -378,6 +381,11 @@ cc.Audio.WebAudio.prototype = {
378381
request.onerror = function () {
379382
cb('request error - ' + url);
380383
};
384+
if (request.ontimeout === undefined) {
385+
request._timeoutId = setTimeout(function () {
386+
request.ontimeout();
387+
}, request.timeout);
388+
}
381389
request.ontimeout = function () {
382390
cb('request timeout - ' + url);
383391
};

0 commit comments

Comments
 (0)