Faster response

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Faster response

Post by icesolid »

I am just wondering how I can achive a faster response for xmlhttp.status.

I want to reduce the amount of time a script will try and retrieve a response from the server. I want it to tell me whether the xmlhttp.status == 200 or NOT instantly.

I do a test where I unplug my Internet while I have my script up and I try to run the PHP end of the program and see how long it takes for the javascript script to determine that the xmlhttp.status is NOT 200. It takes awhile. Anyways to speed this up? Maybe reduce the amount of time javascript tries to get a 200 response?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Faster response

Post by pickle »

The time it takes to get a response is the time it takes for your browser to send a request to the web server and get a response back. There is nothing you can do in Javascript land to make that happen any faster. If you want to speed it up, get a faster Internet connection & get a faster web server.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Faster response

Post by califdon »

I think I see what you're asking; if the server doesn't respond, you want a timeout, right? I'll defer to ~pickle on the details, but I think there might be a way of using settimeout() to halt the execution of your xmlhttp function if no response has been received after so many seconds.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Re: Faster response

Post by icesolid »

Thats exactly what I am talking about.

How would I implement that?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Faster response

Post by califdon »

I haven't done this, so this is mostly just the way I would try to do it:

Since the manual says that settimeout("

Code: Select all

",[time in seconds]) will delay the execution of the action or function by that number of milliseconds, I would try ending the xmlhttprequest function if the desired time is exceeded. If it receives a response within that time, it will end anyway, so it won't have any effect, but if it exceeds it, it will just end the function's execution. Probably something like:[syntax=javascript]...
ajaxRequest.onreadystatechange = function () {
   settimeout("return False;",30000);
   ...
}[/syntax]
See [url]http://www.w3schools.com/js/js_timing.asp[/url]
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Faster response

Post by kaszu »

If it receives a response within that time, it will end anyway, so it won't have any effect, but if it exceeds it, it will just end the function's execution.
Sorry, but 'return False;' won't work, bacause onreadystatechange function is called several times (each time ready state changes), each time it will take only few milliseconds to execute (usually) and after those few milliseconds "return false" will be useless since function already ended. If function takes longer than 30 seconds then setTimeout won't be executed until function ends anyway (only 1 code fragment can run at a time), so unfortunately it's useless.

Try something like this:

Code: Select all

//...
function onTimeout () {
    //Stop ajax request, correct version of "return False;"
    ajaxRequest.stop();
}
function onReadyStateChange () {
    if (ajaxRequest.readyState == 4) {
        //Make sure onTimeout is not called
        clearTimeout(timer);
    }
}
 
//Set timeout to capture 'timeout' :/
var timer = setTimeout(onTimeout, 30000);
 
//Send request
ajaxRequest.onreadystatechange = onReadyStateChange;
ajaxRequest.send(null);
 
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Re: Faster response

Post by icesolid »

Thanks for the help. I used some of your ideas above to come up with a solution.

One small correction to the code above. Use xmlhttp.abort(); NOT xmlhttp.stop();

.stop(); would not cancel the request but .abort(); does.

Once again thanks for the help.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Faster response

Post by califdon »

I sure overlooked that issue. I guess if I had actually tried that, it would have become apparent. Thanks.
Post Reply