Jquery ajax getting the XMLHttpRequest state

931 views Asked by At

Instead of the typical "Loading..." or animated gif, I want to display a better description of what is going on. (Kind of like what some splash screens do for applications). However, glancing at the Jquery documentation there doesn't seem to be a way to get the change in the XMLHttpRequest state.

What I would like to do is something like this

if case = 1 display "Starting"

if case = 2 display "Getting Closer"

if case = 3 display "I'm almost there"

if case = 4 display content

Am I overlooking something or should I abandon jquery's ajax methods and write my own?

1

There are 1 answers

2
Pawel Krakowiak On BEST ANSWER

You need to poll a web service in some time intervals and just return whatever seems appropriate to your application (like the messages you presented) and update the progress indicator. The process is two-fold: first step is to actually initiate the operation that must run and second step is to constantly poll the server to see if the operation is complete.

However, you would have to somehow store the current operation status on the server. I don't know which technology you're working with, so I can only tell you to use something that your framework of choice provides, but make sure the process state is set in a variable that can be persisted (HTTP is stateless), in example in a session variable. Synchronizing this value between the running process and the polling method may be tricky.

In overall, such splash screens are rather used in desktop applications or in web applications when the processing is performed on the client. I don't think it's a good idea to try to have an updating progress when the method you're running asynchronously resides on the server. A progress indicator like this would be good when you're loading assets (like images or other files) and you can count the currently loaded items/size vs. the total size and so on. No matter whether the processing operation will be invoked on the server or on the client it must be countable...

If you don't want yet another dull "Loading..." indicator in your web application, you may randomly display some funny quotes or other texts from an array.

Best solution - minimize the time it takes to process the request. :)