Search code examples
javascriptjqueryspinnerdelay

set a delay time for spinner using jquery


Hi Folks at Stack Overflow. I was looking for a way to add a spinner on my website and I found this useful code but the only thing I'm missing is the code for setting the delay or duration on the spinner, I would really like it to show for like 2 seconds. Any suggestions...

<html>
<head>

<script type="text/javascript">
    $(window).bind("load", function() {
        $('#overlay').fadeOut(function() 
        {   

       $('#container').fadeIn();

        }); 

    });
</script>
</head>
<body>

    <div id="overlay">
         <img src="ajax-loader.gif" alt="Loading" />
         Loading...
    </div>
    <div id="container" style="display:none">

    </div>

</body>
</html>

Solution

  • Click for Demo show loading pic for 3 seconds and then showing the content[Solved]

    HTML

    <div id="overlay">
             <img src="http://cdn.nirmaltv.com/images/generatorphp-thumb.gif" alt="Wait" alt="Loading" />
             <div id="overlayText">
    
    Wait  
    Loading
        </div>
        </div>
        <div id="container"  >
    
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
    

    Jquery

    var delay = 3000;
    setTimeout(function() 
    
        {  
    
            $( "#overlay" ).fadeOut( "slow" );
    
             $('#container').fadeIn();
    
        },
        delay
    ) ;
    

    CSS

    #overlay >img{
    
        position:absolute;
        top:300px;
        left: 320px;
    
        z-index:10;
    
    
    }
    
    
    #overlayText{
         position:absolute;
        top:365px;
        left: 330px;
    
       z-index:11;
    
    }
    #container{
        position:relative;
           display:none;
    }
    #overlay{
    
      width:100%;
        height:100%;
        position: absolute;
        top:0px;
        z-index: 2;
        background-color:#222;
        opacity:0.7;
    
    
    
    }