user277498 user277498 - 3 years ago 197
jQuery Question

Loading spinner gif image gets stuck

I have a 6kb animated gif loading spinner. when I open it in a browser window, it spins fine. But when it opens on an ajax call on my web page, it is stuck. It appears ontime and disapears ontime, but does not spin.

Any ideas?

Here is the jquery code:

$('#please-wait')
.css("visibility", "visible")//.hide()
.ajaxStart(function() {
$(this).css("visibility", "visible");
})
.ajaxStop(function() {
$(this).css("visibility", "hidden");
});


or

$('#please-wait')
.show()
.ajaxStart(function() {
$(this).show();
})
.ajaxStop(function() {
$(this).hide();
});


any ideas?

Answer Source

Here is a jsfiddle example of how it should or could be done.

Here's a snippet

$('#start_loading').click(function(){
    $('#display').html('<img src="http://thinkfuture.com/wp-content/uploads/2013/10/loading_spinner.gif" />');
    setTimeout(function(){
        $('#display').html($('#content').html());
    }, 1000);
});

$('#reset').click(function(){
    $('#display').html('');
});
body{
  background-color: white;
}

#content{
    display:none;
}

#display{
    margin-top: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.6.3.min.js" type="text/javascript"></script>  
<button id='start_loading'>Load text</button>
<button id='reset'>Reset</button>

<div id='display'>
</div>

<div id='content'>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin risus ipsum, hendrerit vitae facilisis sit amet, viverra sit amet elit. Nullam interdum sodales tortor quis suscipit. Donec facilisis condimentum sodales. Nunc imperdiet vehicula aliquam. Etiam non ligula enim, non euismod dui. Aliquam in fermentum neque. Praesent auctor lectus eu magna gravida convallis. Donec semper hendrerit porta. In feugiat tellus a purus vulputate adipiscing. Aenean et risus nec nisl auctor scelerisque. Duis eleifend nunc ut odio consectetur scelerisque.</p><br/>

    <p>Aliquam in scelerisque erat. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aenean dignissim orci at quam placerat a placerat sem porta. Nulla facilisi. Ut ligula lorem, faucibus sit amet eleifend id, fringilla ut orci. Aliquam viverra, enim a auctor tincidunt, neque dui sollicitudin enim, eget feugiat odio lacus cursus lacus. Nunc risus urna, elementum eu vehicula in, fringilla et ante. Aenean iaculis volutpat purus, vitae faucibus justo auctor at. Integer non fringilla nibh.</p><br/>

<p>Quisque elementum, sapien in viverra mollis, magna nisl blandit tortor, ut tincidunt risus nulla id nunc. Fusce rutrum elementum mauris, id aliquam magna varius sed. Ut vestibulum elementum sapien et sagittis. Vestibulum vitae sem arcu. Nulla lacus dui, lacinia vitae dapibus ac, tincidunt vel lectus. In hac habitasse platea dictumst. Curabitur interdum ullamcorper odio, ullamcorper mollis ligula auctor eget. Donec aliquet arcu sed est dignissim pharetra. Praesent sit amet dui venenatis nisl ullamcorper convallis. Morbi rutrum neque id dui venenatis et bibendum felis placerat. </p>
</div>

If you're doing the right parts of JQuery then the odds are your .gif doesn't have a repeat cycle, meaning it only animates once. Since you only hide it instead of adding/removing, it doesn't reset the animation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download