bennyboywonder Posted March 2, 2007 Share Posted March 2, 2007 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(); } Quote Link to comment Share on other sites More sharing options...
fenway Posted March 2, 2007 Share Posted March 2, 2007 You can't use object references in setTimeout/setInterval calls, since they're eval()'ed at a later time by a different (i.e. new) JS interpreter, where "this" has no meaning. Quote Link to comment Share on other sites More sharing options...
bennyboywonder Posted March 2, 2007 Author Share Posted March 2, 2007 how would I go about doing loop like this inside a class then? Quote Link to comment Share on other sites More sharing options...
fenway Posted March 4, 2007 Share Posted March 4, 2007 Not easily... either write your own queuing system, or pass IDs that you can look up on the "other side". Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.