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();
}

I using a long polling script that checks the number of updated records in my database, it works perfectly when theres no update yet, but when I update a record in the database a new ajax request is created with the new count as a parameter, and the first request is still left unchanged. What seems to be the problem here? Is it the way I attached my long polling method to an object? kindly help.

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
  • Is the function CreateAjaxControl() ever being used? I don't see it called anywhere. – Dan Nissenbaum Jan 04 '13 at 04:36
  • Any reason you are not using jquery? Makes things so much easier in the vast majority of cases. – Mr Zorn Jan 04 '13 at 04:49
  • @DanNissenbaum that function is called when a button is clicked in my page, the problem is whenever the parameters in "CheckingProgress()" changes, another ajax request is created with the new parameters – Julius Robles Jan 04 '13 at 05:02
  • The only code that creates a new ajax request is the one line in CreateAjaxControl() – wired_in Jan 04 '13 at 05:10
  • What's the point of the paramater count in CheckingProgress() ? Everytime CheckingProgress() is called, obj.count is the same as the parameter count sent in, thus the reference to count at the end of the function could be replaced with self.count – wired_in Jan 04 '13 at 06:10
  • ok guys got it fixed, thank you for all your ideas it really helped. – Julius Robles Jan 07 '13 at 10:41

1 Answers1

0

What's the point of the paramater count in CheckingProgress() ? Everytime CheckingProgress() is called, obj.count is the same as the parameter count sent in, thus the reference to count at the end of the function could be replaced with self.count

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265