jQuery Method How to - Use delay() to delay an animation








Question

We would like to know how to use delay() to delay an animation.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.5.2.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from  ww  w . j  a  va2 s  . co  m-->
    $(".success, .info, .warning, .error").each(function(){
            $(this).append('<div class="hide">hide</div>');
            $(this).delay(4000).fadeOut();
    });
    $(".hide").live("click", function(){
        $(this).parent().stop().fadeOut();
    });
});
</script>
</head>
<body>
  <div class="success">text</div>
</body>
</html>

The code above is rendered as follows: