Showing a loading spinner only if the data has not been cached.

Posted by Aaron Mc Adam on Stack Overflow See other posts from Stack Overflow or by Aaron Mc Adam
Published on 2010-04-15T20:31:16Z Indexed on 2010/04/15 20:33 UTC
Read the original article Hit count: 281

Hi guys,

Currently, my code shows a loading spinner gif, returns the data and caches it. However, once the data has been cached, there is a flicker of the loading gif for a split second before the data gets loaded in. It's distracting and I'd like to get rid of it. I think I'm using the wrong method in the beforeSend function here:

$.ajax({
                type    : "GET",
                cache   : false,
                url     : "book_data.php",
                data    : { keywords : keywords, page : page },
                beforeSend : function() {                   
                    $('.jPag-pages li:not(.cached)').each(function (i) {                        
                        $('#searchResults').html('<p id="loader">Loading...<img src="../assets/images/ajax-loader.gif" alt="Loading..." /></p>');
                    });
                },
                success : function(data) {  
                    $('.jPag-current').parent().addClass('cached');
                    $('#searchResults').replaceWith($(data).find('#searchResults')).find('table.sortable tbody tr:odd').addClass('odd');
                    detailPage();
                    selectForm();                   
                }
            });

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JavaScript