Page 1 of 1

Faster response

Posted: Thu Jul 02, 2009 9:49 am
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?

Re: Faster response

Posted: Thu Jul 02, 2009 9:51 am
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.

Re: Faster response

Posted: Thu Jul 02, 2009 11:44 am
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.

Re: Faster response

Posted: Thu Jul 02, 2009 6:20 pm
by icesolid
Thats exactly what I am talking about.

How would I implement that?

Re: Faster response

Posted: Thu Jul 02, 2009 10:44 pm
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]

Re: Faster response

Posted: Fri Jul 03, 2009 2:20 am
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);
 

Re: Faster response

Posted: Fri Jul 03, 2009 7:54 am
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.

Re: Faster response

Posted: Fri Jul 03, 2009 11:56 am
by califdon
I sure overlooked that issue. I guess if I had actually tried that, it would have become apparent. Thanks.