I read content from http://www.dave-bond.com/blog/2010/01/JQuery-ajax-progress-HMTL5/ and i try make that ajax progress bar on my own site, but in console i have only xhr response when php script is full completed.
My part of JS code:
$('#userSaveData').on('click', function(e){
e.preventDefault();
var $form = $(this).closest('form');
var route = $form.attr('action');
var form_data = $form.serializeObject();
$.ajax({
xhr: function()
{
var xhr = new window.XMLHttpRequest();
//Upload progress
xhr.upload.addEventListener("progress", function(evt){
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with upload progress
console.log(percentComplete);
}
}, false);
//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;
},
type: 'post',
url: route,//url to my script below
data: form_data, //some data, not used in script below
success: function(){
console.log('completed');
}
});
return false;
});
For example, part of my test php script is:
<?php
usleep(10000000);
echo true;
?>
My current console log first show 1, and then after about 10sec console.log write 'completed'
Simply, i want while ajax request is in progress (php usleep time is about 10sec) display in console percentage of ajax reqest (is that possible?).
Please help.