Question : 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.


Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
---------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 Select All

Answer : What happens if readystatechange doesn't change

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

Random Solutions  
 
programming4us programming4us