Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Web servers can't push unsolicited data to the client; they obey the request-response cycle. The alternative is to use Message Queuing at a significant increase in complexity.</p> <p>Polling from the client is not so bad; web servers are adept at handling many short requests, and a polling interval of 2 or 3 seconds should be fast enough.</p> <p>Here is a polling method I like to use. It asynchronously waits for the response to come back before polling again (requires jQuery):</p> <pre><code>function poll(url, task, progressBar, resultsCallback, timeoutMillis, pollIntervalMillis) { $.ajax({ url: url, type: 'GET', dataType: 'json', timeout: timeoutMillis, data: 'action=poll&amp;task='+task, success: (function(response, status, xhr) { if ('progress' in response) { // update the UI with the progress progressBar.setValue(response.progress); } if ('status' in response) { if (response.status == 'pending') { // task is not finished, continue polling setTimeout((function() { poll(url, task, progressBar, resultsCallback, timeoutMillis, pollIntervalMillis); }), pollIntervalMillis); } else { // task completed if (response.status == 'cancelled') { progressBar.setColor('red'); progressBar.setText("Task '"+task+"' was cancelled"); } else { progressBar.setColor('green'); progressBar.setText("Task '"+task+"' complete"); } // GET the results $.ajax({ url: url, type: 'GET', timeout: timeoutMillis, data: 'action=results&amp;task='+task, success: (function(response, status, xhr) { resultsCallback(response, status, xhr); }), error: error }); } } }), error: error }); function error(xhr, status, err) { alert('Failure to communicate with server: ' + status + ', ' + err); } } </code></pre> <p>And your server-side code should be responding to polls with something like this:</p> <pre><code>{"progress" : 42, "status" : "pending"} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload