Reputation: 639
This is the code:
$.ajax({
type: "POST",
url: url,
xhr: function(e) {
//Download progress
xhr.addEventListener("progress", function(evt){
console.log(evt.lengthComputable);
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with download progress
console.log(percentComplete);
}
});
return xhr;
},
dataType: "json",
success: function(data){
}
});
This is the function I use to retrieve my view from Codeigniter. I want to show a progress bar in the meantime while the page is loading. However when I put the XHR in the AJAX funtion everything stops working. Does anyone have an idea how can I make it run with a progress bar.
Thanks in Advance.
Upvotes: 3
Views: 166
Reputation: 23250
You've missed out a variable declaration so your code doesn't compile.
xhr: function () {
var xhr = new window.XMLHttpRequest(); // <<<<<<< missed this variable
//Download progress
xhr.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with download progress
console.log(percentComplete);
}
}, false);
return xhr;
},
Note that if evt.lengthComputable
is false then you cannot measure the progress of the request.
Upvotes: 2