Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-21-2006, 04:32 PM   PM User | #1
Beagle
Senior Coder

 
Join Date: Jul 2005
Location: New York, NY
Posts: 1,084
Thanks: 4
Thanked 19 Times in 19 Posts
Beagle is an unknown quantity at this point
Aborting XMLHttpRequests in FF

Here's the problem:

I'm making an acynchronous call using an XHR based on user behavior. If this behavior occurs in rapid succession, I want to abort the currently running request and make a new one. Example, fast-typers using a type-ahead control. Now, I've seen solutions using timeouts, but I don't want to solve this with timeouts if I can help it.

So, I have a function that aborts the request before sending a new one. The problem is that when the abort occurs readyState is set to 4, and the status property becomes unavailable, seemingly permanently. In order to work around the readyState being set to 4, before aborting, I wipe the readyStateChange handler (as discussed in the link below). But when I make a new request on the same XHR and set a new readyStateChange handler, the readyState is still 4 even though I just made a new request that won't get a response for another 5 seconds (my endpoint has a sleep in it for testing).

So I thought I'd test for status == 200 but that throws an exception, stating that status is not available. I could wrap a try-catch around it but, the readyState is 4 (completed) and the status is completely unavailable so I have no way of really figuring out what's going on if use a try-catch. I'd almost never be able to detect real errors from this errant behavior.

This is on Firefox 1.5, here is a link to quirksmode discussing this topic: http://www.quirksmode.org/blog/archi...notes_a_1.html

Here's my code:

Code:
var pipe = new XMLHttpRequest();
pipe.open("GET", "http://localhost/sleep1.php", true);

pipe.send(null);

pipe.onreadystatechange = function() {
    if (pipe.readyState == 4)    // *** 4 = REQUEST_COMPLETE ***
    {
        if (pipe.status == 200)
            alert(pipe.readyState + " PIPE SAYS! " + pipe.responseText);
    }
};

pipe.onreadystatechange = function() {};
pipe.abort();

// ***** pipe = new XMLHttpRequest(); **** This will make everything all better, but so costly and shouldn't be necessary!!!

pipe.open("GET", "http://localhost/sleep10.php", true);
pipe.send(null);
pipe.onreadystatechange = function() {
    if (pipe.readyState == 4)    // *** 4 = REQUEST_COMPLETE ***
    {
        if (pipe.status == 200)
            alert("PIPE SAYS! " + pipe.responseText);
    }
};
I would greatly appreciate links to anything discussing this topic, as well as anyone's advice, knowledge, or working solutions to the problem. Is there anything I can do short of creating a new XHR for the new request? Once you abort a request, can it be reused?
Beagle is offline   Reply With Quote
Old 06-21-2006, 05:20 PM   PM User | #2
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
put the send method after you set the onreadystatechange....

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 06-21-2006, 05:52 PM   PM User | #3
Beagle
Senior Coder

 
Join Date: Jul 2005
Location: New York, NY
Posts: 1,084
Thanks: 4
Thanked 19 Times in 19 Posts
Beagle is an unknown quantity at this point
Done. Doesn't change a thing. All the problems I described above are happening exactly the same way.
Beagle is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:16 AM.


Advertisement
Log in to turn off these ads.