2013-01-04 46 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代替。

相关问题