AJAX read data in a progressive manner not just wh

2019-03-22 04:31发布

I like to create a progress bar for my ajax calls. For this I can make my server side script to return the state of it's progress. So I need for javascript to read this progress level and show it.

Is it possible or am I on the wrong road?

2条回答
Juvenile、少年°
2楼-- · 2019-03-22 04:48

You could try something like this (some pseudocode, assuming jQuery, since you've tagged 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
  }
});
查看更多
聊天终结者
3楼-- · 2019-03-22 05:08

Either poll at intervals, returning the payload on the final successful call, or run two AJAX calls - the first retrieving the actual data, and the second polling for update percentage.

查看更多
登录 后发表回答