@@ -801,25 +801,51 @@ cc.loader = (function () {
801
801
} ;
802
802
} else {
803
803
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 ) ;
805
807
if ( xhr . _timeoutId >= 0 ) {
806
808
clearTimeout ( xhr . _timeoutId ) ;
807
809
}
810
+ else {
811
+ xhr . removeEventListener ( 'timeout' , timeoutCallback ) ;
812
+ }
808
813
if ( xhr . readyState === 4 ) {
809
814
xhr . status === 200 ? cb ( null , xhr . responseText ) : cb ( { status :xhr . status , errorMessage :errInfo } , null ) ;
810
815
}
811
816
} ;
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
+ }
813
826
cb ( { status : xhr . status , errorMessage : errInfo } , null ) ;
814
827
} ;
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 ) ;
815
841
if ( xhr . ontimeout === undefined ) {
816
842
xhr . _timeoutId = setTimeout ( function ( ) {
817
- xhr . ontimeout ( ) ;
843
+ timeoutCallback ( ) ;
818
844
} , xhr . timeout ) ;
819
845
}
820
- xhr . ontimeout = function ( ) {
821
- cb ( { status : xhr . status , errorMessage : "Request timeout: " + errInfo } , null ) ;
822
- } ;
846
+ else {
847
+ xhr . addEventListener ( ' timeout' , timeoutCallback ) ;
848
+ }
823
849
}
824
850
xhr . send ( null ) ;
825
851
} else {
@@ -836,10 +862,15 @@ cc.loader = (function () {
836
862
xhr . open ( "GET" , url , true ) ;
837
863
xhr . responseType = "arraybuffer" ;
838
864
839
- xhr . onload = function ( ) {
865
+ var loadCallback = function ( ) {
866
+ xhr . removeEventListener ( 'load' , loadCallback ) ;
867
+ xhr . removeEventListener ( 'error' , errorCallback ) ;
840
868
if ( xhr . _timeoutId >= 0 ) {
841
869
clearTimeout ( xhr . _timeoutId ) ;
842
870
}
871
+ else {
872
+ xhr . removeEventListener ( 'timeout' , timeoutCallback ) ;
873
+ }
843
874
var arrayBuffer = xhr . response ; // Note: not oReq.responseText
844
875
if ( arrayBuffer ) {
845
876
window . msg = arrayBuffer ;
@@ -848,17 +879,38 @@ cc.loader = (function () {
848
879
xhr . status === 200 ? cb ( null , xhr . response ) : cb ( { status :xhr . status , errorMessage :errInfo } , null ) ;
849
880
}
850
881
} ;
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
+ }
852
891
cb ( { status :xhr . status , errorMessage :errInfo } , null ) ;
853
892
} ;
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 ) ;
854
906
if ( xhr . ontimeout === undefined ) {
855
907
xhr . _timeoutId = setTimeout ( function ( ) {
856
- xhr . ontimeout ( ) ;
908
+ timeoutCallback ( ) ;
857
909
} , xhr . timeout ) ;
858
910
}
859
- xhr . ontimeout = function ( ) {
860
- cb ( { status : xhr . status , errorMessage : "Request timeout: " + errInfo } , null ) ;
861
- } ;
911
+ else {
912
+ xhr . addEventListener ( ' timeout' , timeoutCallback ) ;
913
+ }
862
914
xhr . send ( null ) ;
863
915
} ,
864
916
0 commit comments