I attempted to synchronize async Ajax calls using the following JS blocks:
=============== =============== =============== =======
[CODE=javascript]function getXMLHTTPReque st()
{
try
{
req = window.XMLHttpR equest ? new XMLHttpRequest( ): new ActiveXObject(" Microsoft.XMLHT TP");
}
catch (err) { }
return req;
}
// update the window status
function updateStatus()
{
window.status += '-';
}
var httpReq = null;
function loadURL(dest, trigfunc)
{
// create only when needed
if (httpReq == null) { alert("Create httpReq"); httpReq = getXMLHTTPReque st(); }
while (1)
{
// only sumbit another Ajax calls when the readsyState is 0
if (httpReq.readyS tate == 0)
{
httpReq.onready statechange = trigfunc;
httpReq.open("G ET", dest, true);
httpReq.send(nu ll);
break;
}
// delay the while loop execution for 500 milisec by updating the status line
else window.setTimeo ut("updateStatu s()", 500);
}
}
// Ajax triggered function processes different business logic depending on the
// content of responseText
function trigFunc()
{
if (httpReq.readyS tate == 4)
{
// Process the responseText from httpReq
............... ...
// to reset the httpReq.readySt ate to 0 so next Ajax calls can be submitted
httpReq.abort() ;
}
}
function myFunc()
{
// Test for some condition on the web page
if (condition is true)
loadURL(urlOne, trigFunc);
loadURL(urlTwo, trigFunc);
}
[/CODE]=============== =============== ===============
I thought after the loadURL(urlOne, trigFunc) call made, loadURL(urlTwo, trigFunc) call will be made but never submitted until the triggered function from the first call executed and reset the readyState to 0.
While the first call is executed on the server side, window will stay in the loop and update the status.
IT NEVER WORKED. For some reason, the result of second call is lost and window stuck in the forever loop.
Can anyone offer some hint to my problem ? Thanks
=============== =============== =============== =======
[CODE=javascript]function getXMLHTTPReque st()
{
try
{
req = window.XMLHttpR equest ? new XMLHttpRequest( ): new ActiveXObject(" Microsoft.XMLHT TP");
}
catch (err) { }
return req;
}
// update the window status
function updateStatus()
{
window.status += '-';
}
var httpReq = null;
function loadURL(dest, trigfunc)
{
// create only when needed
if (httpReq == null) { alert("Create httpReq"); httpReq = getXMLHTTPReque st(); }
while (1)
{
// only sumbit another Ajax calls when the readsyState is 0
if (httpReq.readyS tate == 0)
{
httpReq.onready statechange = trigfunc;
httpReq.open("G ET", dest, true);
httpReq.send(nu ll);
break;
}
// delay the while loop execution for 500 milisec by updating the status line
else window.setTimeo ut("updateStatu s()", 500);
}
}
// Ajax triggered function processes different business logic depending on the
// content of responseText
function trigFunc()
{
if (httpReq.readyS tate == 4)
{
// Process the responseText from httpReq
............... ...
// to reset the httpReq.readySt ate to 0 so next Ajax calls can be submitted
httpReq.abort() ;
}
}
function myFunc()
{
// Test for some condition on the web page
if (condition is true)
loadURL(urlOne, trigFunc);
loadURL(urlTwo, trigFunc);
}
[/CODE]=============== =============== ===============
I thought after the loadURL(urlOne, trigFunc) call made, loadURL(urlTwo, trigFunc) call will be made but never submitted until the triggered function from the first call executed and reset the readyState to 0.
While the first call is executed on the server side, window will stay in the loop and update the status.
IT NEVER WORKED. For some reason, the result of second call is lost and window stuck in the forever loop.
Can anyone offer some hint to my problem ? Thanks
Comment