3

I want to know that suppose you did an ajax request to a page that runs PHP code. The page outputs some data (using flush() method or otherwise) but because of a 30second timeout or some other error, the php request ends.

Now when the request ends, I want to find out about it on client side and restart the request.

i.e suppose I have something like this

xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readyState==3 && xmlhttp.status==200){
    document.getElementById("A").innerHTML=xmlhttp.responseText;
    }
    else if(xmlhttp.readyState==4 && xmlhttp.status==200){
    document.getElementById("A").innerHTML=xmlhttp.responseText;
    //plus some additional code to end the process
    }
    else if(xmlhttp.status== SOMETHING /*WHAT DO I ADD HERE TO KNOW THAT THE SERVER HAS TIMED OUT OR SOMETHING SO I CAN RESTART THE XMLHTTP CYCLE */){
    //code to resend xmlhttp request.
    }
    
}
peterh
  • 11,875
  • 18
  • 85
  • 108
Ahmed-Anas
  • 5,471
  • 9
  • 50
  • 72
  • In your question you are asking for the check to be on the server side but in your code you have space in the javascript for this. Since your ajax is async, it could be fired off and then the user could leave the page. The script would not have a chance to execute again or notify the server that there was a timeout experienced. You would literally have to force the user to stay on your page until a response came back. You may have to approach this issue by tracking it on the server instead of js. – Tim Joyce Jan 01 '13 at 13:17
  • my bad, that was a typo. corrected. also, I want the check to be on client side. if the user leaves the page, dosnet really matter what happens. – Ahmed-Anas Jan 01 '13 at 13:36

1 Answers1

0

One strategy I used was to set a timer in JS, and then clear it if the call was successful.

var myTimer = setTimeout(stuffToDoOnFailure, 6000); // 6 secs

ajaxCall(function callBack() {
    clearTimeout(myTimer);
});

EDIT:

Of course, if the ajax call succeeds after 6 secs, you might end up with both stuffToDoOnFailure and callBack being executed, so you want to handle that case somehow. That depends on your app, though.