Jump to content

[SOLVED] javascript OOP


bennyboywonder

Recommended Posts

ok. I have been creating a javascript class for ajax calls.

 

part of the class is going to be a method that calls the load method then loops back on itself. however whenever I try to execute one method from inside the loop method, I just get errors.

 

here is the code in question

htRequest.prototype.startLoop = function(lTime, obj)
    {
        setInterval("this.load()", 1000);
    }

 

and here is the entire class code. The errors I keep getting (when adding try/catch handlers) are things like "this.load is not an object" etc etc etc

 

HELP ME PLEASE this is annoying the SHIT out of me

 

/********************/

function htRequest(page, baseQuery, rType, method, loopTime) /*

     - my xmlHTTPrequest class

    constructor variables:
STRING page - the page to make the request to
STRING baseQuery - the initial querystring to send to server not including any additional variables added to the object instance later
STRING rType - the type of data the call will return. Currently either "json", "xml" or "text";
INT method - which httprequest method to use. 1, 2 or 3 for "GET" "POST" or "HEAD" respectively
INT loopTime - how long in milliseconds to wait before looping the callback function;


/*******************/

{
    htRequest.prototype.constructUrl = function() // Construct the url for a GET call
    {
        if(this.baseQuery)
        {
            this.query = this.baseQuery;
            if(this.addQuery) this.query += "&" + this.addQuery;
        }
        else if(this.addQuery) this.query += this.addQuery;
        if(!this.query || this.method != "GET") this.url = this.page;    
        else this.url = this.page + "?" + this.query;
        return this.url;
    }
    // Sends the xmlHTTPrequest
    htRequest.prototype.load = function()
    {
        var rLoaded = this.reqLoaded;
        var eHandle = this.errorHandler;
        var callType = this.callType;
        var rType = this.rType;
        this.constructUrl();
        var http = createXMLHttpRequest();
        http.open(this.method, this.url, true);
        if(this.method == "POST") http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        http.onreadystatechange = function()
        {
    if (http.readyState != 4) return;
    if (http.status != 200)
            {
	if (eHandle) eHandle(http.status);
                return;
            }
            
            switch(rType)
            {
                case "json": var response = eval('('+ http.responseText +')'); break;
                case "xml": var response = http.responseXml; break;
                case "text": var response = http.responseText;
            }
            
            if(rLoaded) rLoaded(response);
        }
        if (this.method == "POST") var sendString = this.query;
        else var sendString = null;
http.send(sendString);
if(this.whileLoading) this.whileLoading();
    }
    
    // Start a callback loop that executes the send method every (loopTime) milliseconds
    
    
    htRequest.prototype.startLoop = function(lTime, obj)
    {
        setInterval("this.load()", 1000);
    }



    //Initialise variables
    if (rType) this.rType = rType;
    else this.rType = "json"; // JSON is the default response type
    this.loopTime = loopTime;
    if (!method) method = 2;
    switch(method) {
        case 1: this.method = "GET"; break;
        case 2: this.method = "POST"; break;
        case 3: this.method = "HEAD"; break;
    }
    this.baseQuery = baseQuery;
    this.addQuery = null;
    this.page = page;
    this.constructUrl();
}

Link to comment
https://forums.phpfreaks.com/topic/40877-solved-javascript-oop/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.