[Javascript] AJAX app causing excessive browser memory usage

Rick Emery rick at emery.homelinux.net
Fri May 19 09:31:50 CDT 2006


Quoting Matt Warden <mwarden at gmail.com>:

>> I'm actually not creating a new one every 30 seconds; I create the xhr
>> object when the page first loads, and reuse it every 30 seconds to  send
>> the request (and process the response).
>
> I would suggest not doing this. The object is meant to represent a
> request, not a connection. At minimum, the object and all its request
> and response content must remain in memory indefinitely rather than only
> for x milliseconds while you do your processing. At worst, the designers
> of the object assumed a short lifecycle and coded accordingly.

I had no idea that I was going to have such a hard time implementing  
this. Basically, I have a function that handles the send (which gets  
called every 30 seconds) and a function that handles the response. I  
want to create the xmlhttprequest object in the send function and  
destroy it in the response function, so it needs to be available to  
both. For some reason, I'm finding it exceptionally difficult to pull  
this off.

The last thing I tried was to create an object:

function xhrObj() {
    this.isXHRBusy = false;
}

xhrObj.prototype.getList() {
    if (!this.xhr) {
       this.xhr = commonUtils.getHTTPObject();
    }
    if (!this.isXHRBusy && this.xhr) {
       this.xhr.open("GET", "getXMLeventlist.php", true);
       this.xhr.onreadystatechange = this.handleXHRResponse;
       this.isEventListWorking = true;
       this.xhr.send(null);
    }
}

xhrObj.prototype.handleXHRResponse() {
    if (HTTP_REQ_COMPLETE == this.xhr.readyState) {
       // process the response
    }
}

When the response function checks readyState, I get a javascript error  
"this.xhr has no properties". I thought I needed to set xhr to  
something either in the constructor or by adding it to the  
xhrObj.prototype, but what would I set it to? Setting it to the  
xmlhttprequest object defeats the purpose of creating and destroying  
it on the fly.

I'm sure I'm just missing something simple...can anybody help me out?

Thanks in advance,
Rick
-- 
Rick Emery

"When once you have tasted flight, you will forever walk the Earth
  with your eyes turned skyward, for there you have been, and there
  you will always long to return"
                                               -- Leonardo Da Vinci




More information about the Javascript mailing list