AJAX reads data in a progressive way, not only after its completion

I like to create a progress bar for my ajax calls. To do this, I can make my server side script to return the status of its progress. So I need javascript to read this level of progress and show it.

Is this possible or am I mistaken?

+3
source share
2 answers

You can try something like this (some pseudocode suggesting jQuery, since you marked the question as such):

var poll;
$.ajax({
  url: 'your_ajax_script',
  beforeSend: function(){ // set up out-of-band status polling
    poll = setInterval( function(){
      $.get('your_script_that_returns_status',
        function(data){ 
          update_progressbar(data);
        });
      }, 1000 ); // update every second?
  },
  success: function(data) {
    clearInterval( poll ); // stop polling
    finalize_or_hide_progressbar(); // clean up
    do_something_with( data ); // your "done" logic
  }
});
+1
source

, AJAX - , - .

0

Source: https://habr.com/ru/post/1772472/


All Articles