2012-08-31 40 views
1

我有功能很多此代碼爲我的web應用程序沒有互聯網連接和超時的XMLHTTPRequest的腳本

function ui_editlocation(id){ 

var hr = new XMLHttpRequest(); 

var url = "scripts/ui_processing.php"; 

var ui = "editlocation"; 

var vars = "ui="+ui+"&id="+id; 

hr.open("POST", url, true); 

hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

hr.onreadystatechange = function() { 

    if(hr.readyState == 4 && hr.status == 200) { 

     var return_data = hr.responseText; 

     document.getElementById("ui_content").innerHTML = return_data; 

    } 

} 

hr.send(vars); 

document.getElementById("ui_content").innerHTML = "Loading..."; 

} 

我想,如果互聯網連接中斷時,得到一個消息,說「請添加一些代碼檢查您的互聯網連接,請點擊這裏再試一次'

如果有互聯網連接,我希望它在30秒後超時並說'我們的網絡目前正在經歷高需求,請點擊這裏再試'

我需要'點擊h再次嘗試'重新發送請求

我知道這是一個很大的問題,但任何幫助,將不勝感激。

感謝 卡爾

回答

2
function ui_editlocation(id){ 
var hr = new XMLHttpRequest(); 
var timeout=setTimeout(function(){ 
    hr.abort(); 
    timeout=null; 
    if(confirm("Do you want to try again?")){ui_editlocation(id);} 
},30000); 

var url = "scripts/ui_processing.php"; 

var ui = "editlocation"; 

var vars = "ui="+ui+"&id="+id; 

hr.open("POST", url, true); 

hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

hr.onreadystatechange = function() { 

    if(hr.readyState == 4 && hr.status == 200) { 
     if(timeout){cancelTimeout(timeout);timeout=null;} 

     var return_data = hr.responseText; 

     document.getElementById("ui_content").innerHTML = return_data; 

    } 

} 

hr.send(vars); 

document.getElementById("ui_content").innerHTML = "Loading..."; 

}