How to show loading animation before printing buffer content from ob_start()?
I have a script that runs for quite a while and I am using ob_start()
to buffer the output and print the result after the script finishes executing.
I need to show some loading animation while the script executes and replace it with the output when the s开发者_开发问答cript finishes executing.
I know that it can be done quite beautifully with JavaScript and jQuery. I was just wondering whether it can be done using PHP alone?
Is there any library that allows PHP to do this?
Unfortunately you can't use PHP (Which is running on the server) to alter the HTML that has already been sent to the client.
In other words you are probably going to have to use JavaScript to achieve what you want to do.
You can't replace content you have already sent with just PHP. You can only append to it. I don't see any practical way of doing what you want to do with PHP alone.
You could try something like this:
<!Doctype>
<title>hide later</title>
<p>Wait 3 seconds</p>
<?php
ob_start();
sleep(3);
?>
Done!
<style>p{display:none}</style>
<?php
ob_end_flush();
But … it works in Opera only. :) IE, WebKit and Gecko will wait for the last bit before they render anything. Oh, and it’s invalid markup.
精彩评论