Mountain Mountain - 10 months ago 23
Ruby Question

To continually pull result from the server via Ajax until timeout or getting result

I would like to make an Ajax request to the server, which replies a job status and result. If the status is "Error" or "Ok", the client should show the result (containing HTML code) to the user. However, if the status is "Waiting", the client should wait for a few seconds and then automatically send the same Ajax request again. This should continue until it either times out or gets a "Error"/"OK" message.

Any idea for a solution/code snippet like this? I am currently using Rails, JQuery, and Prototype. Many thanks.



You can do like

(function refresh(){
      url:      '/myfile',
      data:     'data',
      dataType: 'text',
      type:     'POST',
      success:   function(data){
          if(data === 'Waiting')
             setTimeout(refresh, 5000);

That would initiate a self-executing anonymous function with an ajax request. If the returned string equals Waiting it will set a timeout for 5 seconds and calls itself again.