I'm trying to figure out how to iterate over an array that would send
a series of XMLHttp requests. I'd like to have each request finish
before the next one begins, but I believe that this is not possible to
do in a straightforward fashion, given the normally asynchronous
method of XMLHttp. The (untested, pseudo) code below gives a little
idea of what the goal is, but I'm not really sure how to program it
elegantly.
One thought I had is to compare two arrays, one with the desired IDs
to be completed, and another with the IDs that have been completed.
The callback handler would add IDs as each request is completed, and
then restart the process until the two arrays match exactly.
Is there any more elegant way?
Thanks for any pointers.
--Brent
------------------------------------------------------------------------
function updateLists()
{
var listIds = "3,7,42,157 ";
var listArr = listIds.split(' ,');
for(i = 0; i < listArr.length; i++)
{
doUpdate(listAr r[i]); //<--How do I make the loop wait for
doUpdate() to complete before proceeding?
}
}
function doUpdate(listId )
{
//executes XMLHttp request on server and sets up callback
handler, showListStatus
}
function showListStatus( callbackRespons e)
{
//Code that updates page with result from server.
//This could add to an array of finished IDs, and then call
updateLists() again, where
//the array of finished IDs would be compared to the array of
desired IDs
}
a series of XMLHttp requests. I'd like to have each request finish
before the next one begins, but I believe that this is not possible to
do in a straightforward fashion, given the normally asynchronous
method of XMLHttp. The (untested, pseudo) code below gives a little
idea of what the goal is, but I'm not really sure how to program it
elegantly.
One thought I had is to compare two arrays, one with the desired IDs
to be completed, and another with the IDs that have been completed.
The callback handler would add IDs as each request is completed, and
then restart the process until the two arrays match exactly.
Is there any more elegant way?
Thanks for any pointers.
--Brent
------------------------------------------------------------------------
function updateLists()
{
var listIds = "3,7,42,157 ";
var listArr = listIds.split(' ,');
for(i = 0; i < listArr.length; i++)
{
doUpdate(listAr r[i]); //<--How do I make the loop wait for
doUpdate() to complete before proceeding?
}
}
function doUpdate(listId )
{
//executes XMLHttp request on server and sets up callback
handler, showListStatus
}
function showListStatus( callbackRespons e)
{
//Code that updates page with result from server.
//This could add to an array of finished IDs, and then call
updateLists() again, where
//the array of finished IDs would be compared to the array of
desired IDs
}
Comment