I am using jQuery's ajax to run a php script with the below:
jQuery.ajax({
type: "POST",
url: "my_php_script.php",
success: function( response ) {
console.log(response);
},
error: function(){
console.log('error is...');
}
});
my_php_script.php loops through an array and on each product runs a function:
$count = count($array);
$i = 0;
foreach ($array as $item) {
myFunction($item);
$i++;
if($i == $count){
echo json_encode('all done and a success');
}
}
This all works but the script can generally take around 2-3 minutes to finish so I need to show the progress for the viewer whilst it runs. How can i do this?
I can get the percent of the loop by adding
$percent = intval($i/$count * 100)."%";
echo json_encode($percent);
into my php loop as so:
$count = count($array);
$i = 0;
foreach ($array as $item) {
myFunction($item);
$percent = intval($i/$count * 100)."%";
echo json_encode($percent);
$i++;
if($i == $count){
echo json_encode('all done and a success');
}
}
But how can i get this value in my jQuery ajax call?
progresscallback on the ajax call to show this?