Update a Web Page as a Process Runs

前端 未结 3 1579

I have a page where a multitude of time consuming functions occur. What I would like to do is as each step of the process is completed, update the web page to let the user k

3条回答
  •  失恋的感觉
    2020-12-22 07:19

    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.

    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.

    Here is a polling method I like to use. It asynchronously waits for the response to come back before polling again (requires jQuery):

    function poll(url, task, progressBar, resultsCallback, 
            timeoutMillis, pollIntervalMillis) {
        $.ajax({
            url: url,
            type: 'GET',
            dataType: 'json',
            timeout: timeoutMillis,
            data: 'action=poll&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&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);
        }
    }
    

    And your server-side code should be responding to polls with something like this:

    {"progress" : 42, "status" : "pending"}
    

提交回复
热议问题