I want to perform an operation and simultaneously show the progress in HTML5 and JavaScript. Since I am using IE 8, the <progress>
element is not supported, the approach I thought of is using a jQuery AJAX method like this:
<div class="progress-bar" role="progressbar" id="id" aria-valuemin="0" aria-valuemax="100">
<span class="sr-only">Complete</span>
</div>
$(document).ready(function(){
$("button").click(function(){
$.ajax({
url: "Perform a server side operation",
success: function(result){
ProgressBarUpdate(20);
}
});
});
});
var ProgressBarUpdate = function(value) {
value = value + 10;
$("#id").css("width", value +"%");
}
But the issue here is how to perform the operation and update the progress bar simultaneously? Because after coming to success:
part of the AJAX request, directly showing 100%
progress doesn't make sense. Is there any other approach available for this?
The jQuery library provides a way to make any callback function waiting for multiple Ajax calls. This method is based on an object called Deferred. A Deferred object can register callback functions based on whether the Deferrred object is resolved or rejected. Here is an example of the Deferred object.
The ajax() method in jQuery is used to perform an AJAX request or asynchronous HTTP request. Parameters: The list of possible values are given below: type: It is used to specify the type of request. url: It is used to specify the URL to send the request to.
The jqXHR (jQuery XMLHttpRequest) replaces the browser native XMLHttpRequest object. jQuery wraps the browser native XMLHttpRequest object with a superset API. The jQuery XMLHttpRequest (jqXHR) object is returned by the $. ajax() function. The jqXHR object simulates native XHR functionality where possible.
Assuming the operation is taking place server side, I know of two approaches.
Polling
This will take place with a different ajax
setInterval(function(){
$.ajax("checkprogress.url", function(data){
$('.progress-bar').val(data);
}, 1000);
Basically, you will poll a server-side service/script/code every interval and the response will contain the current progress. You will then update the progress bar.
WebSockets
Another approach is to use a web-socket framework which will allow you to push content(progress updates) and/or implement a remote procedure call to conected clients in real-time.
Two such frameworks are SignalR and socket.io
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With