Skip to content

Commit d3ae844

Browse files
committed
Improve loader listeners
1 parent 0a707e9 commit d3ae844

File tree

1 file changed

+64
-12
lines changed

1 file changed

+64
-12
lines changed

CCBoot.js

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -801,25 +801,51 @@ cc.loader = (function () {
801801
};
802802
} else {
803803
if (xhr.overrideMimeType) xhr.overrideMimeType("text\/plain; charset=utf-8");
804-
xhr.onload = function () {
804+
var loadCallback = function () {
805+
xhr.removeEventListener('load', loadCallback);
806+
xhr.removeEventListener('error', errorCallback);
805807
if (xhr._timeoutId >= 0) {
806808
clearTimeout(xhr._timeoutId);
807809
}
810+
else {
811+
xhr.removeEventListener('timeout', timeoutCallback);
812+
}
808813
if (xhr.readyState === 4) {
809814
xhr.status === 200 ? cb(null, xhr.responseText) : cb({status:xhr.status, errorMessage:errInfo}, null);
810815
}
811816
};
812-
xhr.onerror = function () {
817+
var errorCallback = function () {
818+
xhr.removeEventListener('load', loadCallback);
819+
xhr.removeEventListener('error', errorCallback);
820+
if (xhr._timeoutId >= 0) {
821+
clearTimeout(xhr._timeoutId);
822+
}
823+
else {
824+
xhr.removeEventListener('timeout', timeoutCallback);
825+
}
813826
cb({status: xhr.status, errorMessage: errInfo}, null);
814827
};
828+
var timeoutCallback = function () {
829+
xhr.removeEventListener('load', loadCallback);
830+
xhr.removeEventListener('error', errorCallback);
831+
if (xhr._timeoutId >= 0) {
832+
clearTimeout(xhr._timeoutId);
833+
}
834+
else {
835+
xhr.removeEventListener('timeout', timeoutCallback);
836+
}
837+
cb({status: xhr.status, errorMessage: "Request timeout: " + errInfo}, null);
838+
};
839+
xhr.addEventListener('load', loadCallback);
840+
xhr.addEventListener('error', errorCallback);
815841
if (xhr.ontimeout === undefined) {
816842
xhr._timeoutId = setTimeout(function () {
817-
xhr.ontimeout();
843+
timeoutCallback();
818844
}, xhr.timeout);
819845
}
820-
xhr.ontimeout = function () {
821-
cb({status: xhr.status, errorMessage: "Request timeout: " + errInfo}, null);
822-
};
846+
else {
847+
xhr.addEventListener('timeout', timeoutCallback);
848+
}
823849
}
824850
xhr.send(null);
825851
} else {
@@ -836,10 +862,15 @@ cc.loader = (function () {
836862
xhr.open("GET", url, true);
837863
xhr.responseType = "arraybuffer";
838864

839-
xhr.onload = function () {
865+
var loadCallback = function () {
866+
xhr.removeEventListener('load', loadCallback);
867+
xhr.removeEventListener('error', errorCallback);
840868
if (xhr._timeoutId >= 0) {
841869
clearTimeout(xhr._timeoutId);
842870
}
871+
else {
872+
xhr.removeEventListener('timeout', timeoutCallback);
873+
}
843874
var arrayBuffer = xhr.response; // Note: not oReq.responseText
844875
if (arrayBuffer) {
845876
window.msg = arrayBuffer;
@@ -848,17 +879,38 @@ cc.loader = (function () {
848879
xhr.status === 200 ? cb(null, xhr.response) : cb({status:xhr.status, errorMessage:errInfo}, null);
849880
}
850881
};
851-
xhr.onerror = function(){
882+
var errorCallback = function(){
883+
xhr.removeEventListener('load', loadCallback);
884+
xhr.removeEventListener('error', errorCallback);
885+
if (xhr._timeoutId >= 0) {
886+
clearTimeout(xhr._timeoutId);
887+
}
888+
else {
889+
xhr.removeEventListener('timeout', timeoutCallback);
890+
}
852891
cb({status:xhr.status, errorMessage:errInfo}, null);
853892
};
893+
var timeoutCallback = function () {
894+
xhr.removeEventListener('load', loadCallback);
895+
xhr.removeEventListener('error', errorCallback);
896+
if (xhr._timeoutId >= 0) {
897+
clearTimeout(xhr._timeoutId);
898+
}
899+
else {
900+
xhr.removeEventListener('timeout', timeoutCallback);
901+
}
902+
cb({status: xhr.status, errorMessage: "Request timeout: " + errInfo}, null);
903+
};
904+
xhr.addEventListener('load', loadCallback);
905+
xhr.addEventListener('error', errorCallback);
854906
if (xhr.ontimeout === undefined) {
855907
xhr._timeoutId = setTimeout(function () {
856-
xhr.ontimeout();
908+
timeoutCallback();
857909
}, xhr.timeout);
858910
}
859-
xhr.ontimeout = function () {
860-
cb({status: xhr.status, errorMessage: "Request timeout: " + errInfo}, null);
861-
};
911+
else {
912+
xhr.addEventListener('timeout', timeoutCallback);
913+
}
862914
xhr.send(null);
863915
},
864916

0 commit comments

Comments
 (0)