Antonio F.
Antonio F.

Reputation: 73

Ghost Ajax request

I'm developing a website using PHP+Ajax. I have to fire a ajax request that may take long time to be processed (about 5 min), I do not need any response from this request so the user should be free to navigate into my website while the server is executing this ajax request in background.

My Code:

 //I can easly lunch the ajax request

$.ajax({
    type: 'GET',
    url: "myurl.php",
    async: true,
    success: function (data) {}
});   

this script works fine but as said it takes several minutes before finishing, and in this while the user cannot open any page of my domain. It looks like my browser is locked waiting for the response.

At the moment to "unlock" my browser i have the following solutions:

-restart my web server

-restart the browser

-wait until the script is done

Is there a trick or something fire "ghost" ajax requests?

(ghost means that i do not want to wait for the response)

I tryed to fire the request and than call the abort() function, but it abort all the request also the execution of the PHP script.

any tips?

PS. i tested it with Firefox 8, Chrome 15.0 and IE9

Upvotes: 0

Views: 301

Answers (2)

Aurimas Ličkus
Aurimas Ličkus

Reputation: 10074

You could do different way, for ex. with simple queue:

  1. Send request to server to do some job, save it to database (with instructions what need to be done) add flag for ex completed = 0
  2. Create cron job on server every 5 min, (prob with lockrun)
  3. Cron job runs and checks database if there incompleted tasks and runs each one by one
  4. Marks it as completed = 1.
  5. You then can find if task is completed by flag (and prob userID) and notifify user that task is completed.

This way user can navigate your site without problems, you can even provide progress to user how much is completed if you need it, by updating db during process.

Upvotes: 0

rabusmar
rabusmar

Reputation: 4143

If you are using sessions in your background script, you should call session_write_close when you finish processing session vars, because a session cannot be open by more than one script at any given time.

Upvotes: 2

Related Questions