tags:

views:

62

answers:

3

I am looking for a way to show in a webapp in front of a task a wait-message and after it hide the message. The task is running a longer time. I dont know if it is possible at all.

The problem is, so far I can see, that the site will be returned to the users browser AFTER the task is completed because the task is part of the site as a inline code replaces by the webserver interpreter (no matter if PHP, Perl or whatever).

The only solution I can imagine is to parallel the task with threads or processes and requery the state with ajax in the website.

Any idea to do it less complex? Thanks for help!

A: 

The server should start sending content to the browser as soon as your code starts executing; it does not wait for the server-side code to finish first.

Therefore, you can just run the task after rendering the HTML containing the wait message. After the task, you can render a <script> block that hides the wait message.

SLaks
I tried this but it did not work. I thought therefore the browser starts displaying not before the end of the site is reached.
chris
It works for me. What language are you using? Does it have buffering options or a `flush` method?
SLaks
Are you running CGI? php won't stream in cgi mode unless you use fastCGI or somthing.
Byron Whitlock
I use mason. It is a template engine over mod_perl.
chris
AHHHH! i found a flush function in mason. thanks for help!!
chris
A: 

Serveral ways you can do this:

1) Pull the task out into its own file so it isn't inline. Then use asycronous AJAX to query the task. Display a progress bar on firing of the event, and hide it whne it is done.

2) Make sure you are streaming your data back to the browser (This is the default in php.). If you are running php as a CGI it will execute the whole script before returning anything to the browser. In this case you will have to o #1 Put the inline task as the LAST thing on the page. In the place where the inline task is now, put up a <div id=loading>Content Loading Please Wait</div> or spinner/progress bar.

Capture the output of the inline task into a php variable At the end of the task add <script>document.getElementById('loading').innerHTML ="$taskResult";</script>

Byron Whitlock
I tried this. And that will not show my wait-div.------------------<div id="x" style="display:none;">wait</div><script>document.getElementById("x").style.display='block';</script>% sleep (10);<script>document.getElementById("x").style.display='none';</script>------------------------------------
chris
A: 

See Watching long processes through CGI. You could adapt the same technique to display updates using AJAX calls.

Sinan Ünür