Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
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
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.