jQuery Event How to - Delay before running javascript code








Question

We would like to know how to delay before running javascript code.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.8.3.js'></script>
<style type='text/css'>
.test {<!--   w w w  . j a  v a 2  s . co  m-->
  width: 100px;
  height: 100px;
  background: red;
}
</style>
<script type='text/javascript'>
$(window).load(function(){
    $(function(){
        $('.test').hide(1000,function(){
            console.log("hide is complete");
        });
    });
});
</script>
</head>
<body>
  <div class="test">some</div>
</body>
</html>

The code above is rendered as follows: