Here it is possible.
There is a file loadable.php
, which does the same process:
<? $from = 0; $to = 10000; $progress_update = 100; header('Content-Length: '.ceil(($to - $from) / $progress_update), true, 200); set_time_limit($to + 10); for ($i = $from; $i < $to; $i++) { usleep(100); if ($i % $progress_update == 0) { echo ' '; ob_flush(); flush(); } } die(' '); ?>
And there is a file that, when accessing the file loadable.php
, calls this long process by an asynchronous request and shows the progress of the execution:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <progress id="progress" min="0" max="1" value="0"></progress> <script> var timer_progress; var progress = $('#progress'); $.ajax ({ type: 'GET', dataType: 'plain', url: 'loadable.php' , beforeSend: function(thisXHR) { timer_progress = setInterval(function() { if (thisXHR.readyState > 2) { var totalBytes = thisXHR.getResponseHeader('Content-Length'); var dlBytes = thisXHR.responseText.length; progress .attr('max', totalBytes) .attr('value', dlBytes); } }, 200); }, complete: function() { clearInterval(timer_progress); progress.attr('value', progress.attr('max')); }, }); </script>
The first file in the Content-Length
header sends the exact duration of the operation in bytes, and as the operation proceeds, it sends a space to the output of 1 byte.
The second file tries to load the first (sluggish) file, as it “downloads”, displaying how many bytes from those specified in the Content-Length
loaded.