Order Prototype's Ajax.Request in a kind of Pipeline

K

Kreisquadratur

I'm in the situation, that a user could take actions which will
trigger several Ajax.Requests. The problem here is, that the browser
should stay responsive, so I need the asynchronous feature but the
responses from the server should not only have the same order as the
requests, but I also have to wait for the first answer before I can
send the second request. So like:

Request 1
Response to 1
Request 2
Response to 2
Request 3
Response to 3

Not:

Request 1
Request 2
Request 3
Response 1
Response 3
Response 2

And not even:

Request 1
Request 2
Request 3
Response 1
Response 2
Response 3

Why? Because the server responding me will lose/"forget" his current
answer, if another request comes in _and_ he can't even handle two
almost parallel requests.

I don't know how to change my code, cause functions like onSuccess
seem to run in a seperate "thread".

Anybody any idea?
 
T

Tom de Neef

Kreisquadratur said:
I'm in the situation, that a user could take actions which will
trigger several Ajax.Requests. The problem here is, that the browser
should stay responsive, so I need the asynchronous feature but the
responses from the server should not only have the same order as the
requests, but I also have to wait for the first answer before I can
send the second request. So like:

Request 1
Response to 1
Request 2
Response to 2
Request 3
Response to 3

The XMLHttpRequest function has a onreadystatechange property which you can
use to test if it has completed successfuly (readyState=4). Put your
requests in an array and process the elements of this array in the event
handler. Somethin like:
// req assigned to the XMLHttpRequest object
var requests = [url1,url2,url3]
requests.reverse()
req.onreadystatechange = nextRequest

function nextRequest()
{ if ((req.readyState!=4) || (requests.length==0)) return
req.open("GET",requests.pop(),true)
}

Tom
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top