Answer the question
In order to leave comments, you need to log in
How to restart XMLHTTPRequest request?
There is a piece of a script which does XMLHTTPRequest request. From time to time, the server processing the request hangs. And then the script hangs. You have to manually restart.
How can the XMLHTTPRequest request be improved so that if something happens (in the absence of a response from the server) it will restart.
Initially the code is like this:
var xhr = new XMLHttpRequest();
xhr.open('GET', diceLink);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4){
r=xhr.responseText;
}
};
xhr.send()
Answer the question
In order to leave comments, you need to log in
var timeout;
function send() {
var xhr = new XMLHttpRequest();
xhr.open('GET', diceLink);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4){
clearTimeout(timeout);
r = xhr.responseText;
}
};
xhr.send();
// ждем 10 сек... не лучшее решение, но как ещё — хз.
timeout = setTimeout(function(){
xhr.abort(); // Останавливаем
send(); // Запрашиваем снова
}, 10000);
}
send();
and some kind of answer should still come, that the server is not responding !! Here, focus on it. If the server does not respond, then re-run the script
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question