New ajax request needs to wait the previous one finished?

Hi guys,

I met a problem about ajax request.

The situation is like this: When first load the page, i made a long connection with the backend using ajax(loop at the backend without repsonse to the client). And the ajax request continue

running. And when i made another new ajax request, this new one will wait for the previuos one finish, then it

could run. is there any tips for this? or is there any yii configuration or server configuration for this?

Thanks in advance…

Hi and welcome to the forum

Ajax requests are made from jQuery…

Remember Yii is just a wrapper for these… to understand ajax you need to read/learn jQuery…

Regarding ajax requests they are all asynchronous by default - http://api.jquery.com/jQuery.ajax/

If you need to make a second ajax only when the first one finishes… you can call the second one from the "complete" or "success" event of the first one…

or use deferred objects - http://api.jquery.co…eferred-object/

Yeap, i called it like that.

maybe i didn’t explain it very clear.

Situation:

Js:

OnPageLoad:

make a ajax call to the backend action named actionA

$.ajax({… }). This call is mean to tell the php side to loop to read from the database, if there is something new, echo to frontend, otherwise continue looping to check. In frontend, there is also another

ajax call to submit message to the backend.

Backend:

Action controller/actionA (Sample):

<?php

if(isset($_GET[‘msg’])) {

// write into db

die();

}

$hasMsg = false;

while(!$hasMsg) {

// loop to check whether there is new msg

if($hasMsg) {

echo 'has new';


break;

}

usleep(1000000);

}

Yii::app()->end();

The problem is that when the page first loads, the first ajax is waiting for the php to check the new msg.

And if there is no new, it will continue checking. Meanwhile i make another new ajax call(submit msg to backend), this call will get the reponse until the first ajax finished. I used the default options for jquery.ajax and it should be async… I don’t know why this happened…

Thanks

could be the usleep()… as this function delays program execution… why not call from jQuery on set intervals ?

that is just for not giving too much pressure to the db

I was just thinking that this function prevents the other ajax call… worth trying without it…

Could be that you use sessions… check this discussion - http://stackoverflow.com/questions/4900214/ajax-request-are-not-async

NOTE: moved to proper section (General Discussion for Yii 1.1 instead of Tips, Snippets, Tutorial)

I think you should use CDbSession as app session. I read an article about that, but now i can not remember it. Try with it i think it should work.

Every time when you ask by ajax then framework runs session(opens session), and next time the opening session will be waited until first request has finished.

Yeah, got it.

The problem is that the session file is locked while the request is running, and next request is always

waiting for the unlock of that file, so we need session_write_close()

Maybe the CDbSession can also help in this situation.

really appreciated~-~