1

I want to show a loading image before heavy data process and remove it after done, so I wrote this code:

$("a#myId").click(function(){
    $(this).append('<img src="loading.gif" />');
    ...
    ... doning some heavy data process (that take more that 5 seconds) here ...
    ...
    $("img[src='loading.gif']").remove();
});

But the loading does not show! (when I leave $(this).append('<img src="loading.gif" />'); and remove next lines, loading image display correctly)

3
  • what kind of data processing are you doing? Commented Nov 22, 2012 at 14:28
  • 1
    the best way is to add a "loading" class to your "a" element and remove it... Commented Nov 22, 2012 at 14:29
  • thanks silly, your simple way solve my problem. it seems that add/remove element to/from DOM, need more longer time and cpu in compare with add/remove class. Commented Nov 28, 2012 at 13:06

2 Answers 2

1

Just tested this and it works. http://jsfiddle.net/Z3MXJ/

<a id="myId" href="#">Click me</a>
<div id="target"></div>
<script>
$("#myId").click(function(){
    $('#target').append('<img src="http://www.consafelogistics.com/gfx_portal/loading.gif" />');

    var url = 'http://onerahi.cabu.school.nz/files/2012/09/goes-12-firstimage-large081701.jpg',
        img = $('<img>');
        img.hide();
        img.bind('load', function(){$(this).fadeIn();
            $("img[src$='loading.gif']").hide();
        });
        $('body div#target').append(img);
        img.attr('src',url);
});
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

You can use a deferred object

function processData( ) { ... }

$("a#myId").click(function(){

    $(this).append('<img src="loading.gif" />');
    $.when( processData ).then( function( ) { 
        $("img[src='loading.gif']").remove();
    });

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.