E
E
Express7772016-03-18 10:33:25
JavaScript
Express777, 2016-03-18 10:33:25

How to clear xhr.responseText on onprogress event?

Hello.
There is a simple PHP code that displays answers in a loop:

ob_start();

    $x = range(0, 20);

    foreach ( $x as $value )
    {
      usleep(250 * 1000);
      echo $value;
      flush();
      ob_flush();
    }


The client requests this page via ajax:
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>

<script>
  window.onload = function(  ) {
    $.ajax({
      url: '/parser/ajax.php',
      xhr: function() {
        var xhr = new window.XMLHttpRequest();
        xhr.onprogress = function( event ) {
          console.log(xhr.responseText);
        };
        return xhr;
      },
    });
  };
</script>


The task outputs each iteration in the loop to the browser.
Now displays
0
01
012
0123
01234
012345

That is, it concatenates the server output ( xhr.responseText )
You need to get a separate iteration at each step:
0
1
2
3
4
5


How can I do that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-03-18
@alexey-m-ukolov

You just need to clear the buffer before outputting: ob_clean() .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question