2013-01-04 45 views
0
我使用長輪詢腳本來檢查的在我的數據庫更新的記錄數
function AjaxRequest(params, url) { 
    if (params) { 
     this.params = params; 
     this.type = "GET";   
     this.url = url; 
//  this.contentType = "multipart/form-data"; 
     this.contentLength = params.length;; 
    } 
} 

AjaxRequest.prototype.createXmlHttpObject = function() { 
    try { 
     this.xmlHttp = new XMLHttpRequest(); 
    } 
    catch (e) { 
     try { 
      this.xmlHttp = new ActiveXObject("Microsoft.XMLHttp"); 
     } 
     catch (e) {} 
    } 

    if (!this.xmlHttp) { 
     alert("Error creating XMLHttpRequestObject"); 
    } 
} 

AjaxRequest.prototype.process = function() { 
    try { 
     if (this.xmlHttp) { 
      document.getElementById("loading"+this.params).innerHTML = "loading..."; 
      document.getElementById("loading"+this.params).className = "loading"; 
      this.xmlHttp.onreadystatechange = this.handleRequestStateChange(); 
      this.xmlHttp.open(this.type, this.url, true); 
      this.xmlHttp.setRequestHeader("Content-Type", this.contentType); 
      this.xmlHttp.setRequestHeader("Content-Length", this.contentLength); 
      this.xmlHttp.send(this.params); 
      } 
     } 
     catch (e) { 
      document.getElementById("loading"+this.params).innerHTML = ""; 
      alert("Unable to connect to server"); 
     } 
    } 

AjaxRequest.prototype.handleRequestStateChange = function() { 
    var self = this; 
    return function() { 
     try { 
      if (self.xmlHttp.readyState == 4 && self.xmlHttp.status == 200) { 
       self.handleServerResponse(); 
      } 
     } 
     catch (e) { 
      alert(self.xmlHttp.statusText); 
     } 
    }; 
} 

AjaxRequest.prototype.handleServerResponse = function() { 
    try { 
     if(this.xmlHttp.responseText.length > 0){ 
      document.getElementById("loading"+this.params).innerHTML = this.xmlHttp.responseText; 
     } 
    } 
    catch (e) { 
     alert("Error reading server response"); 
    } 
} 

function CreateAjaxControl(params, url){ 
    var con = $("#"+params+" select").val(); 
    url += "?id="+params+"&con="+con; 
    var ajaxRequest = new AjaxRequest(params, url);  
    ajaxRequest.createXmlHttpObject(); 
    ajaxRequest.process(); 
    ajaxRequest.count = 0; 
    ajaxRequest.progress = CheckingProgress; 
    ajaxRequest.progress(ajaxRequest, ajaxRequest.params, ajaxRequest.count); 
    //var ajaxRequest = new AjaxRequest(params, url); 
    //ajaxRequest.checking = setInterval(function(){CheckingProgress(ajaxRequest.params);}, 100); 

} 

//function Check(id){ 
// var res = 0; 
// while(res != "done..."){ 
//  res = CheckingProgress(id, res); 
// } 
//} 


function CheckingProgress(obj, id, count){ 
    var self = obj; 
    if (window.XMLHttpRequest){ 
     xmlhttp8 = new XMLHttpRequest(); 
    }else{ 
     xmlhttp8 = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp8.onreadystatechange = function(){ 
     if (xmlhttp8.readyState==4 && xmlhttp8.status==200){ 
      var result = xmlhttp8.responseText.split(','); 
      document.getElementById("loading"+id).innerHTML = result[0] + " out of " + result[1]; 
      self.count = result[0]; 
      self.progress(self, self.params, self.count); 
     }else if(xmlhttp8.status==404){ 
      document.getElementById("loading"+id).innerHTML = "done..."; 
      //return "done"; 
     } 
    } 
    xmlhttp8.open("GET","views/test2.php?id="+id+"&count="+count,true); 
    xmlhttp8.send(); 
} 

,它完美的作品即使世界上沒有更新呢,但是當我更新的記錄數據庫會以新計數作爲參數創建新的ajax請求,並且第一個請求仍保持不變。這裏似乎是什麼問題?這是我把我的長輪詢方法附加到一個對象的方式嗎?善意幫助。長輪詢Ajax請求被添加每次參數更改

+0

函數CreateAjaxControl()是否曾經正在使用?我沒有看到它叫任何地方。 –

+0

任何你不使用jQuery的原因?在絕大多數情況下使事情變得更容易。 –

+0

@DanNissenbaum在我的頁面中單擊按鈕時會調用該函數,問題是每當「CheckingProgress()」中的參數發生更改時,將使用新參數創建另一個Ajax請求 –

回答

0

CheckingProgress()中的參數計數值是什麼?每次調用CheckingProgress()時,obj.count與發送的參數計數相同,因此在函數結尾的計數引用可以用self.count代替。

相關問題