5

enter image description here

  • I retrieve about 15,000 rows from my database every time I visit this page.
  • The page might take about 8-10 seconds to finish load everything - I currently, use DataTable.
  • I think it would be nice to show user any kind of loading feedback during that time.

  • I want to create my own loading animations, and chose my own color, style, and size.

enter image description here

  • I'm not if I use any Ajax call.
  • I am just retrieving a lot of data out of my database.

What is the most efficient way to show loading animation while retrieving data from database ?

iori
  • 3,236
  • 12
  • 43
  • 78
  • If you are not using Ajax, then how are you doing it? – RobertoNovelo Feb 27 '15 at 21:40
  • 3
    `(function () { // start animation })(); window.onload = function () { // remove animation };` – i-- Feb 27 '15 at 21:41
  • 1
    I'm 99% sure you are using Ajax if you are using JavaScript to download your data. – sebnukem Feb 27 '15 at 21:48
  • @RobertoNovelo : I'm sorry. I edited my post might have use Ajax, that I might not aware of. You're probably right ! – iori Mar 02 '15 at 14:00
  • @sebnukem : I used Laravel-4 , PHP framework – iori Mar 02 '15 at 14:01
  • Try putting your loading stuff in a webworker. That way your page will be responsive whilst everything is loading. – Tschallacka Mar 02 '15 at 14:18
  • 1
    you SHOULD use an asynchronous call if you don't want the page to freeze... ajax calls are a perfect fit for this.. just set the loader before the line before you execute the call and remove the loader once the callback comes back... webworkes might work but looks like a little overkill doesn't it? – VDP Mar 02 '15 at 14:35
  • I really like your suggestion. I think I should stick with that. Do you mind answer it ? I know how to show the loading animation, but I am not sure how to hide it back. – iori Mar 02 '15 at 15:02
  • But why the hell you are downloading 15,00 row??? How you can even display 15,00 rows on screen? And how will wait for it 8-10s? It bad ux and bad business approach . Download 20 on the beginning and next 20 on scroll and this will work fast and will be user friendly. – Kinga Mar 06 '15 at 11:08
  • If you read my post clearly, I used DataTable, and DataTable is loading all the data all at once in the beginning, unless, you decided to do it Ajax called and only query whatever you typed in the search box - that is different story here. – iori Mar 06 '15 at 13:10

4 Answers4

5

To begin with, the most simple solution is to use ajax call to retrieve the table rows populated by php.

JSFiddle


SIMPLE:

main.html / main.php

/*This makes the timeout variable global so all functions can access it.*/
var timeout;

/*This is an example function and can be disregarded
This function sets the loading div to a given string.*/
function loaded() {
  $('#loading').html('The Ajax Call Data');
}

function startLoad() {
    /*This is the loading gif, It will popup as soon as startLoad is called*/
    $('#loading').html('<img src="http://rpg.drivethrustuff.com/shared_images/ajax-loader.gif"/>');
    /*
    This is an example of the ajax get method, 
    You would retrieve the html then use the results
    to populate the container.
    
    $.get('example.php', function (results) {
        $('#loading').html(results);
    });
    */
    /*This is an example and can be disregarded
    The clearTimeout makes sure you don't overload the timeout variable
    with multiple timout sessions.*/
    clearTimeout(timeout);
    /*Set timeout delays a given function for given milliseconds*/
    timeout = setTimeout(loaded, 1500);
  }
  /*This binds a click event to the refresh button*/
$('#start_call').click(startLoad);
/*This starts the load on page load, so you don't have to click the button*/
startLoad();
img {
  width: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id='start_call'>Refresh</button>
<div id='loading'></div>

An example of the php would look something like this

example.php

/*Database call to get list*/
foreach($list as $li){
    echo "<tr><td>$li[var1]</td><td>$li[var2]</td></tr>";
}

ADVANCED:

A more advanced way to load your content is to use webworkers and multiple database calls segregated into small chunks.

You would set up web-workers to do small 100 row results and use LIMIT in your sql statements to page the results into small chunks. Then you can page through the first 100 results while the other results are loading.

This process is more difficult and takes longer to implement, but leads to seamless and quick loading.


EDIT:

If you want to change the loading animation just change the URL. and if you want the URL to be loaded on page load put it in the div itself.

/*This will make the img load on page load rather than DOM execution.*/
<div id='loading'>
    <img src="http://rpg.drivethrustuff.com/shared_images/ajax-loader.gif"/>
</div>

The Image doesn't have to be an image. It can be any loading icon you want. A gif was quick and dirty. You could use something like font-awesome spinner

Community
  • 1
  • 1
Roger
  • 3,226
  • 1
  • 22
  • 38
  • 1
    Well done, so far I really like your answer, this is exactly what I am looking for. I will make sure to accept this soon. I have 2 quick questions for you. 1. If I want to change style, I just need to change the url to the `src` right ? does it have to be .gif ? – iori Mar 02 '15 at 14:47
  • 2. Is there a way to display my loading animation a little earlier ? I notice it's display for about 4 seconds after 11 seconds that I hit the refresh button on my browser. I want it to display as soon as I hit the refresh botton. Can I do that base on your snippet ? – iori Mar 02 '15 at 14:50
4

data.php to check out the DB and build the table's HTML

function getData(){

    //div contains the loader
    $('#loader').show();

    $.get('clients/data.php', function(data) {
        //
        $('#content').html(data);
        //hide the loader
        $('#loader').hide();
        //build DataTable
        $('#content table').dataTable();

    });
}

getData();
Niahm
  • 424
  • 5
  • 11
2

This depends on what language you use, but the fundamentals are the same. You load the page with just the animation while the query completes, and then replace the animation with the content.

In jQuery this probably means linking the animation in plain HTML, then separately calling the database with AJAX. When you get the result, you can use jQuery append to target the content area, and write into that in real time.

I include PHP since you say that you are not using AJAX, but in PHP the structure is the same: You would need to link the image, flush the page so that it displays, and then execute your query. Cover the animation with negative CSS margin-top on the search results, and Bob is your uncle.

1

Your question :

"I want to create my own loading animations, and chose my own color, style, and size."

You should visit http://www.ajaxload.info/ there you can chose,customize and download loading gif really fast.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Medo
  • 21
  • 2