How to display script server progress in jQuery?

With this code, I can show an animated gif while the script server is running:

function calculateTotals() { $('#results').load('getResults.php', null, showStatusFinished); showLoadStatus(); } function showLoadStatus() { $('#status').html(''); } function showStatusFinished() { $('#status').html('Finished.'); } 

However, I would like to display the status of how far the script is, for example. โ€œProcessing line 342 of 20,000 ...โ€ and count it until it is complete.

How can i do this? I can create a script server that constantly contains updated information, but where can I put this command to read this, say, every second?

+6
jquery ajax
source share
4 answers

After reading your comments, Andrew respond.

You would read this status:

 function getStatus() { $.getJSON("/status.php",{"session":0, "requestID":12345}, function(data) { //data is the returned JSON object from the server {name:"value"} setStatus(data.status); window.setTimeout("getStatus()",intervalInMS) }); } 

Using this method, you can open several simultaneous XHR requests on the server.

all your status.php for output:

 {"status":"We are done row 1040/45983459"} 

However, you can output as much information as possible in response and process it accordingly (for example, provide a progress bar or perform animation).

For more information on $ .getJSON see http://docs.jquery.com/Ajax/jQuery.getJSON

+2
source share

I do not understand the specifics of jQuery, but the general answer, which is not related to the wold poll, should be: Use variation forever . Basically, create a hidden iframe and set src to getresults.php. Inside getresults, you "flows" back to script blocks that are calls to the javascrpt function in the parent document, which actually updates the progress. Here is an example that shows the main idea behind an eternal frame. (I would not recommend using its actual JS or HTML, although it's fairly average)

+1
source share

Your server side of the script must somehow advance somewhere on the server (file, field in the database, memcached, etc.).

You must have an AJAX function that returns the current progress. Interrogate this function once per second and make the result accordingly.

0
source share

Not knowing how your code works on the server side, it's hard to say. However, the process consists of three stages. First, you need to invoke the creation of the script job. This returns the id number and sets up the server. Then, every second or so, you need to call up a status script that returns a status message that you want to display. This script status should also return a value indicating whether the job has finished or not. When the state of the script says that the task is completed, you stop polling.

How you get this status script means to know that the status of the job depends heavily on how the server is configured, but it probably involves writing a message to the database table at different points during the job. Then the script status reads this message from the database.

0
source share

All Articles