Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ajax Advice 1

Status
Not open for further replies.

jammer1221

Programmer
Jul 30, 2003
352
US
Hi guys,

Looking for a little advice. Here is the standard code I'm using to send an Ajax request:
Code:
function sendRequest() {
http=createAjaxObj()
if (http) {
    http.open("GET","url", true);
    http.onreadystatechange = function() {
    if (http.readyState == 4) {
        if(http.status == 200) {
        results = http.responseText;
        http=null;
        }
    }
}
    http.send(null);
}

In a certain page I was creating I needed to send a request every 5 seconds almost indefinitely. But, doing so caused huge memory leaks. So, I added "http=null" to release the memory before I sent a new request. Now, I am curious, is it in the correct place to maximize results? Or should I, for example, put it right before I create a new ajax object?

Really, what I'm asking, does the above code look decent or would you suggest I change something?

Thanks for the advice,

Matt
 
I don't understand how you can send the string "url" in the open command and actually get results.

[monkey][snake] <.
 
ahh, I see.

As to where you are assigning http = null, I think that's the best place to do it, as opposed to assigning it to null right before you reassign it.

I'm pretty sure there will be no difference in performance issues putting that in either place.

[monkey][snake] <.
 
Sorry for the delay. monksnake, I very much appreciate your advice and I'm glad to hear I've been putting it in the right place.

Thanks,

Matt Powell
 
Jammer1221, let me comment on your code if I may. After looking through the snippet again, you may want to do this:

Code:
function sendRequest() {
http=createAjaxObj()
if (http) {
    http.open("GET","url", true);
    http.onreadystatechange = function() {
    if (http.readyState == 4) {
        if(http.status == 200) {
        results = http.responseText;
        http=null;
        }
    }
    [!]http.send(null);[/!]
}
    [s]http.send(null);[/s]
}

If for some reason you don't enter the if(http) block, you would try to use a send command on something that wouldn't understand .send.

So for that reason, you want to put the http.send(null) within the if block to where you know http will hit the .open command.

[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top