How To: Create a Simple Visual “Loading” Effect

If you’ve got a script that takes a while to load and want to make your user aware that everything is alright, then this tool is for you. The idea is simple - display the message before the labor intensive part of the script runs, and then using CSS after the labor intensive part of the script, hide the message.

If you’re using a language that provides a buffer (ASP for instance), make sure you turn the buffer off first, or this won’t have any effect. (eg. Response.Buffer = False)

Example (See it in action):

1
2
3
4
5
6
7
<p id="loading_msg"><img src="http://img520.imageshack.us/img520/4950/loadingvp7.gif" alt="" style="vertical-align: middle;" /> Loading...</p>
<?php
// Code that takes forever here, could of course be any language
sleep(5); // Only for demonstration
?>
<style type="text/css">#loading_msg { display: none; }</style>
Woohoo, five seconds passed.

Leave a Reply