4

In traditional javascript AJAX, we know if readystate is:

  • 0 - The request is not initialized
  • 1- The request has been set up
  • 2 - The request has been sent
  • 3 - The request is in process
  • 4 - The request is complete.

When it comes to jQuery AJAX, we have:

  • complete property where we code what should happen after completion
  • success property where we code what should happen if the ajax request succeeds and
  • error property where we code what should happen if ajax request fails.

All of the above properties lets us code to do something after completion of ajax request. Where can I specify some code to execute something during processing(when readyState is 3) in Jquery Ajax??

As my AJAX script takes too long time to execute, which means, I will not attain 'complete' stage quickly. This seems like nothing is happening to the user. I wanted to initiate another ajax script at processing stage which gets information from server meanwhile and shows the user what has been done so far. Is it possible at all in Javascript? I know there is no multi-threading in Javascript.

I think I made my self clear. But, Please let me know if anything is not making any sense.

John Flatness
  • 32,469
  • 5
  • 79
  • 81
murali
  • 227
  • 1
  • 7
  • 17
  • 1
    how it is possible until server will not return the data how ajax will process? so you can use loading image to show user that data is loading and you should check your code to optimize it. – Code Lღver Feb 01 '12 at 06:32
  • Possible duplicate: http://stackoverflow.com/questions/3309185/jquery-ajax-overwrite-onreadystatechange-handler – aorcsik Feb 01 '12 at 06:34

2 Answers2

1

I handle this by initiating the first long running request, returning to the user immediately and allowing the process to fork server side for the extended processing.

The initial return ajax call to the user sets them up to 'watch' that process via a flag against the object ( I store them against the object in the database, but you could for instance watch file sizes or other stuff )

Subsequent ajax calls occur in a loop, each one returning setTimeout for the next call, and report on changes to that flag so the progress of the long running process is then visible. Completion of the long running process prompts NOT sending another setTimeout() and showing the overall results.

If your process is not that intensive, a simple spinner would probably do the job and no work for your server process. I usually handle that having $.ajax flip the visibility of a 'spinner' icon that's preloaded on my pages in the same spot for all.

Daren Schwenke
  • 5,428
  • 3
  • 29
  • 34
0

According to jQuery's Ajax documention, they do not expose the readystate change event:

No onreadystatechange mechanism is provided, however, since success, error, complete and statusCode cover all conceivable requirements.

It would be possible to show a loading image after the initial Ajax request is kicked off (and before getting any "complete" or "success" events, and then start polling a different URL via ajax which will give you the status of the first request, assuming your server can show progress of the long process before it completes.

Nick B
  • 7,639
  • 2
  • 32
  • 28