murali
murali

Reputation: 227

state of XMLHttpRequest Object in jquery AJAX

In traditional javascript AJAX, we know if readystate is:

When it comes to jQuery AJAX, we have:

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.

Upvotes: 4

Views: 3641

Answers (2)

Nick B
Nick B

Reputation: 7689

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.

Upvotes: 0

Daren Schwenke
Daren Schwenke

Reputation: 5478

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.

Upvotes: 1

Related Questions