2011-07-07 198 views
1

如何跟蹤Ajax調用超時下面是一個簡單的AJAX功能Ajax請求超時

function process() 
{ 
    document.getElementById('message').innerHTML='Loading..'; 
    var ajaxRequest; // The variable that makes Ajax possible 
    try 
        { 
     ajaxRequest = new XMLHttpRequest(); 
     } 
    catch (e) 
     { 
      try 
       { 
       ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
       } 
      catch (e) 
       { 
        try 
         { 
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
         } 
        catch (e) 
         { 
         // Something went wrong 
         alert("Your browser broke!"); 
         return false; 
         } 
       } 
     } 
    ajaxRequest.onreadystatechange = function() 
     { 
     if(ajaxRequest.readyState == 4) 
      { 
      var ajaxDisplay = document.getElementById('message'); 
      ajaxDisplay.innerHTML = ajaxRequest.responseText; 
      } 
     } 
    var query; 
      query="process.php?action=delete"; 
    ajaxRequest.open("POST", query, true); 
    ajaxRequest.send(null); 
} 

回答

1

試試這個:

function process() 
{ 
    document.getElementById('message').innerHTML='Loading..'; 
    var ajaxRequest; // The variable that makes Ajax possible 
    try 
        { 
     ajaxRequest = new XMLHttpRequest(); 
     } 
    catch (e) 
     { 
      try 
       { 
       ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
       } 
      catch (e) 
       { 
        try 
         { 
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
         } 
        catch (e) 
         { 
         // Something went wrong 
         alert("Your browser broke!"); 
         return false; 
         } 
       } 
     } 
    ajaxRequest.onreadystatechange = function() 
     { 
     if(ajaxRequest.readyState == 4) 
      { 
      //Ajax successful, we don't need to abort it anymore... 
      clearTimeOut(AjaxTimeOut); 
      var ajaxDisplay = document.getElementById('message'); 
      ajaxDisplay.innerHTML = ajaxRequest.responseText; 
      } 
     } 
    var query; 
    query="process.php?action=delete"; 
    ajaxRequest.open("POST", query, true); 
    ajaxRequest.send(null); 
    //Set when to abort ajax request... 
    var AjaxTimeOut = setTimeOut("function(){ajaxRequest.abort};", 60000); 
}