1

I have looked around for similar topics but none seem to address the problem I am currently having. I have this JavaScript:

<script type="text/javascript">

                http = new XMLHttpRequest();

                function fetch()
                {

                    http.open("GET", "script.php", true);
                    http.onreadystatechange = useHttpResponse;
                    http.send(null);

                }

                function useHttpResponse()
                {

                    if(http.readyState == 4)
                    {

                            var textout = http.responseText;
                            document.getElementById("ajax").innerHTML=textout;

                    }

                }

</script>

Extremely basic stuff. PHP-script is a simple loop:

for($i = 0; $i < 30000; $i++)
{

      echo 'Hello<br />';

}

This works great. I press a button that has onClick="javascript:fetch()" and it requests the PHP-script and outputs 30.000 lines of "Hello" in the div with id "ajax".

The problem is that it has to wait until it has run all 30.000 loops. I want it to output the response via the AJAX request after EACH loop so that the list kind of expands as the script is running. Is this possible? How would I do this? Like I said, I have searched but come up empty. I realize this is strictly a cosmetic effect but I would be greatful for any advices!

Thank you

Tanax
  • 433
  • 2
  • 10
  • 20

2 Answers2

2

disable output buffering in your PHP settings or call http://www.php.net/manual/en/function.flush.php after each iteration.

As pointed out by Mike. in your XHR's onreadystatechange, you're checking for status == 4, which means all the data has been transferred. You should check for status == 3, which means The request is in process; often some partial data is available from the response, but the server isn't finished with its response.

Mike also pointed out that if your server is set to use compression, then all output must not be buffered and you cannot stream your HTML content.

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • 1
    That wouldn't work. `http.readystate` of `4` is when the data is finished transferring. – Mike Aug 29 '11 at 16:42
  • 1
    Also, you should mention that if gzip compression is enabled, calling flush() will have no effect. – Mike Aug 29 '11 at 16:52
  • This worked wonders! I had tried the flush method before but nothing happened until the script had finished all loops. With the status set to 3 instead of 4, it is now outputing everything in each loop. Obviously, with only that for-loop, the browser froze because of all the updates that were sent but with my application script that has less lines to print(but takes more time to run) it is working great. Thanks for the fast reply! – Tanax Aug 29 '11 at 17:07
0

No, it's not possible. You cannot fetch data while the script is running, even if some output has been sent. AJAX can only read the complete result.

See the question I asked on this subject: Long Polling/HTTP Streaming General Questions

Community
  • 1
  • 1
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308