I tried to load a script via ajax call with a timeout, if the calls fail after timeout then I abort the call, but when I inspected the network tab I can see the status of the js is still pending and also the pages loader still loads.
The script file I was trying to load is google map API from China, if the request fails I load the Chinese version of API and it works fine, but it takes more than one minute to stop the page loading and to show the below message.
jquery.min.js:2 GET https://maps.googleapis.com/maps/api/js?key="--mykey---"&libraries=places&_=1553093568934 net::ERR_TIMED_OUT
My code for loading and aborting is below
var xhr = $.ajax({
url: "https://maps.googleapis.com/maps/api/js?key=keyyy&libraries=places",
dataType: "script",
timeout: 5000
}).done(function() {
//do stuff
console.log("loaded");
})
.fail(function() {
//do stuff
console.log("fail");
xhr.abort();
$.ajax({
url: "https://maps.google.cn/maps/api/js?key=keyy&libraries=places",
dataType: "script",
timeout: 5000
}).done(function() {
//do stuff
console.log("loaded");
})
});
I also tried this
var xhr = $.ajax({
url: "https://maps.googleapis.com/maps/api/js?key=key&libraries=places",
dataType: "script",
timeout: 5000,
success:function(){
},
error:function(a,b,c){
//do stuff
console.log("fail");
console.log(a,b,c);
console.log(xhr.state());
xhr.abort();
console.log(xhr.state());
$.ajax({
url: "https://maps.google.cn/maps/api/js?key=key&libraries=places",
dataType: "script",
timeout: 5000,
success:function(){
console.log("loaded chinese")
},
error:function(a,b,c){
}
})
}
})