let's say I have a dummy server side code (ajax_response.php) which looks like this:
<?php
for ($i = 1; $i <= 10; $i++) {
echo $i;
sleep(1);
}
?>
which means after 10 secs it will echo '12345678910'.
I also have the following jquery code:
$.ajax({
type: 'POST',
url: 'ajax_response.php'
}).then(function(results) {
//... success
}, function {
//... execute if error occurs
} );
I can use the above ajax to get the response from ajax_response.php, but the problem is, the success function (ie. the first function inside .then) will only be called when ajax_response.php finished executing (which takes 10 secs).
I would like to know if I can make this more efficient. ie. as soon as echo is called in the php file, I'll immediately get the response from the client side.
It will be great if you can include some sample code.