XMLHttpRequest Object: Asynchronous vs Synchronous Mode

H

HugeBob

Hi All,

I've got a question about Asynchronous vs Synchronous mode with the
XMLHttpRequest object. What are the ramifications of using one mode
vs the other? If the script uses Asynchronous mode, it sounds as if a
thread retrieves the data from the supplied URL and the JS function
that called the open() and send() methods continues on. Where as
using Synchronous mode the method that called open() and send() waits
until the data from the URL has been retrieved. Am I correct in my
thinking?
 
L

-Lost

HugeBob said:
Hi All,

I've got a question about Asynchronous vs Synchronous mode with the
XMLHttpRequest object. What are the ramifications of using one mode
vs the other? If the script uses Asynchronous mode, it sounds as if a
thread retrieves the data from the supplied URL and the JS function
that called the open() and send() methods continues on. Where as
using Synchronous mode the method that called open() and send() waits
until the data from the URL has been retrieved. Am I correct in my
thinking?

http://www.w3.org/TR/XMLHttpRequest/#dfn-send

The ramifications of not using the asynchronous aspect of AJAX is having the UA or browser
block activity until the request returns, finishes, loads, whatever.

I would not say that the open() and send() methods continue. The onreadystatechange
property still receives a dispatch however (even when blocking). So instead of being
notified of what open() and send() are doing you will receive an onload event and the
readyState of 4.

I would say you are incorrect in thinking that it cannot update its interface during a
synchronous call (assuming you really meant onreadystatechange instead of open() and
send()).

I hope I worded all that correctly.

-Lost
 
H

HugeBob

http://www.w3.org/TR/XMLHttpRequest/#dfn-send

The ramifications of not using the asynchronous aspect of AJAX is having the UA or browser
block activity until the request returns, finishes, loads, whatever.

I would not say that the open() and send() methods continue. The onreadystatechange
property still receives a dispatch however (even when blocking). So instead of being
notified of what open() and send() are doing you will receive an onload event and the
readyState of 4.

I would say you are incorrect in thinking that it cannot update its interface during a
synchronous call (assuming you really meant onreadystatechange instead of open() and
send()).

I hope I worded all that correctly.

-Lost

I find myself in the same state as you signature, LOL. Well, sort
of. I've got an app that uses AJAX. The onreadystatechange method
sets a global variable with XML it retrieves from the server when
readyState is 4. The global variable is later searched via JS. The
application works in IE when synchronous mode is used and not at all
in FireFox 1.5 (the onreadystatechange method never seems to run).
So, take the following function:

globalVariable = null;
function ajaxFunction( )
{
var xmlHttp;

xmlHttp = getXMLHTTPRequestObject( );
alert(xmlHttp);
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
globalVariable = xmlHttp.responseText;
}
}

xmlHttp.open("GET","time.asp",true);
xmlHttp.send(null);
}

So, are you saying that even when this function completes,
globalVariable may not have anything in it when it's accessed later
regardless of whether I use asynch or synch mode?
 
L

-Lost

HugeBob said:
I find myself in the same state as you signature, LOL. Well, sort
of. I've got an app that uses AJAX. The onreadystatechange method
sets a global variable with XML it retrieves from the server when
readyState is 4. The global variable is later searched via JS. The
application works in IE when synchronous mode is used and not at all
in FireFox 1.5 (the onreadystatechange method never seems to run).
So, take the following function:

globalVariable = null;
function ajaxFunction( )
{
var xmlHttp;

xmlHttp = getXMLHTTPRequestObject( );
alert(xmlHttp);
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
globalVariable = xmlHttp.responseText;
}
}

xmlHttp.open("GET","time.asp",true);
xmlHttp.send(null);
}

So, are you saying that even when this function completes,
globalVariable may not have anything in it when it's accessed later
regardless of whether I use asynch or synch mode?

No, globalVariable will have the responseText specified by the XMLHttpRequest. However,
if asynchronous is set to false, depending on the size of the server's output (e.g. how
long it takes to load) globalVariable will still have the responseText, it may not be
available as quickly.

Assuming of course that getXMLHTTPRequestObject() returns the proper XMLHttpRequest.

To support the greatest number of possible browsers first have a look at:

http://jibbering.com/2002/4/httprequest.html

....the implementation there is fairly solid.

-Lost
 
H

HugeBob

No, globalVariable will have the responseText specified by the XMLHttpRequest. However,
if asynchronous is set to false, depending on the size of the server's output (e.g. how
long it takes to load) globalVariable will still have the responseText, it may not be
available as quickly.

Assuming of course that getXMLHTTPRequestObject() returns the proper XMLHttpRequest.

To support the greatest number of possible browsers first have a look at:

http://jibbering.com/2002/4/httprequest.html

...the implementation there is fairly solid.

-Lost

Oh, my getXMLHTTPRequestObject() method handles that. I borrowed it
from W3Schools (http://www.w3schools.com/ajax/ajax_server.asp):

function getXMLHTTPRequestObject( )
{
var xmlHttp = null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
}
}
}
return xmlHttp;
}

Though, I'm still confused as to why the onreadystatechange method
isn't being called in Firefox. Excuse me for being so dense. I'm
trying to wrap my brain around this. So, basically, if I reach the
portion of my JS that wants to parse the retrieved XML before
xmlHttp.readyState==4, I'm hosed? I was thinking that using
synchronous mode would avoid this.
 
L

-Lost

HugeBob said:
Oh, my getXMLHTTPRequestObject() method handles that. I borrowed it
from W3Schools (http://www.w3schools.com/ajax/ajax_server.asp):
Though, I'm still confused as to why the onreadystatechange method
isn't being called in Firefox. Excuse me for being so dense. I'm
trying to wrap my brain around this. So, basically, if I reach the
portion of my JS that wants to parse the retrieved XML before
xmlHttp.readyState==4, I'm hosed? I was thinking that using
synchronous mode would avoid this.

Bearing in mind a flawless (near flawless) XHR implementation will not suffer this error.

Could you perhaps offer a page of your implementation for further debugging?

-Lost
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top