March 27, 2012

Spinner Minimum Time Delay Jquery

Question by Both FM

The following java-script is working fine (Jquery)

$(document).ready(function(){
$('#c_area').load($('.m_top:first').attr('href'))   
});
$('.m_top').click( function(){
$("#myDiv").html('<img src="images/loading.gif" alt="Wait" />');
var name= $(this).attr('href');
$('#c_area').hide().load(name).fadeIn('normal');                
$("#myDiv").html('');
return false;
});

I want to show the loading image (spinner) for 2 seconds minimum, because when page has already loaded, while click again on link, loading image (spinner) not display again because the page already loaded.

minimum time = 2 second

maximum time = until the contents loaded

Answer by Starx

Use delay(). Give the number of seconds in milliseconds like

$('#c_area').delay(2000).hide().load(name).fadeIn('normal');   
            //^ delay for 2 sec then hide      

Here is your solution.

I used the setTimeout() to trigger the function after 2 seconds.

$('.m_top').click( function(){          

    var name= $(this).attr('href');
    $('#c_area').html('<img src="images/loading.gif" alt="Wait" />');
    setTimeout( function() {
       $("#c_area").hide().load(name).fadeIn("normal");
    },2000);

    $("#myDiv").html('');
    return false;
});

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!