What happens if readystatechange doesn't change

Hi Experts,

I have been looking everywhere for a timeout event for AJAX, I can't seem to find one.
I have a couple of ideas, and I just wanted to get an opinion, best practice on this.

Reference
http://ajaxblog.com/archives/2005/06/01/async-requests-over-an-unreliable-network

I have tried to implement the above code, I put it inside and outside my Ajax function to no avail.



---------Javascript --------------------
var timeoutId = window.setTimeout(
                                function() {
                                        if ( callInProgress(xmlHttp) ) {
                                                xmlHttp.abort();
                                                xmlHttp.responseText = 'timeout';
                                                eval(varPostFunction);
                                            } //end if 
                                } //end function
                                ,1000 // 3 seconds 
                                ); //end setTimeout
    
function GenericAjax(varURL, varPostFunction) {
var rand = Math.random() * 1000;
varURL = varURL + "&random=" + rand;
var xmlHttp;
 
try
  {
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
 
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      window.clearTimeout(timeoutId);
      eval(varPostFunction);
      }
    }
    
  xmlHttp.open("GET",varURL,true);
  xmlHttp.send(null); 
  
} 
 
 
 
---------Addition to ASPX page to make over 5 seconds to timeout
Dim dtTime As Date
 
        dtTime = Now()
 
        Do While DateAdd(DateInterval.Second, 6, dtTime) > Now()
 
        Loop

Open in new window

LVL 1
Craig LambieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michel PlungjanIT ExpertCommented:
Why not just


xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      eval(varPostFunction);
      }
    }
    else {
      if (!confirm('Keep going? state is='+xmlHttp.readyState)) xmlHttp.onreadystatechange=null
    }

0
Craig LambieAuthor Commented:
Ok, I tried that.
Issue is this, I get a confirm message box all the time
state=1, state =2, state=3, state=1 etc....
I think it is being called over and over, but still it is a bit annoying, and it isn't really a timeout.


xmlHttp.onreadystatechange=function() {
   if(xmlHttp.readyState==4) {
     eval(varPostFunction);
     xmlHttp.readyState = 0;
   } else {
     if (!confirm('Keep going? state is='+xmlHttp.readyState) ) {
	     xmlHttp.onreadystatechange=null;
	     return false;
	   } //end if
   } //end if
 	} //end function
 
--------------------------
I was thinking more like 
......
	  xmlHttp.open("GET",varURL,true);
	  xmlHttp.send(null);
  	
  	setTimeout("KillHTTP("+xmlHttp+")",5000);
} //end function
 
function KillHTTP (xmlHttp) {
 if (xmlHttp.readyState != 4) {
    if (!confirm('Keep going it has been 5 seconds?') ) {
	     xmlHttp.onreadystatechange=null;
	     return false;
    } //end if
	} //end if
} //end function

Open in new window

0
Michel PlungjanIT ExpertCommented:
It was only an example to show you the state DOES change.

Your suggestion is ok
.
0
Craig LambieAuthor Commented:
ok, the issue with my function is that the Object is not passed correctly to KillHTTP ??
How would I do that?
0
Michel PlungjanIT ExpertCommented:
Just don't quote it.

       
        setTimeout("KillHTTP(xmlHttp)",5000);


However since it is global in scope, just don't pass it:


        setTimeout("KillHTTP()",5000);


function KillHTTP () {
 if (xmlHttp.readyState != 4) { // global since defined outside all functions

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.