3

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?

EBAG
  • 18,548
  • 12
  • 51
  • 89

2 Answers2

1

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
  }
});
Ken Redler
  • 22,899
  • 7
  • 55
  • 68
  • That's the problem, I rated you 1 up since it was a new lead, but this method brings another complication since the first script should update the progression level somewhere else for the second script to read, It would be much better if the first script sends the level during the process – EBAG Nov 01 '10 at 14:44
  • It sounds like you're interested in the Comet pattern. Read this: http://en.wikipedia.org/wiki/Comet_(programming), then follow some of the links from this: http://stackoverflow.com/questions/136012/comet-and-jquery. But: avoid needless complexity if you can. – Ken Redler Nov 01 '10 at 15:00
0

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.

Stefan Kendall
  • 61,898
  • 63
  • 233
  • 391