fade out div after x seconds with jquery

michele picture michele · Nov 16, 2012 · Viewed 67.8k times · Source

I do a fade in div that is not displayed when I load the page:

    $('#overlay').fadeIn('fast');
    $('#box').fadeIn('slow');

I would do this instructions after x seconds, doing a fadeOut of the div:

$('#overlay').fadeOut('fast');
$('#box').hide();

How can I do it? Actually fadeOut is done on button click.

The script is here: http://clouderize.it/cookie-localstorage/a.php The div that appear when I click on another image will disappear after x seconds. Thanks a lot.

Answer

Asad Saeeduddin picture Asad Saeeduddin · Nov 16, 2012

The .delay method is purpose built for what you are describing:

$('#overlay').fadeIn('fast').delay(1000).fadeOut('fast');
$('#box').fadeIn('slow').delay(1000).hide(0);

http://jsfiddle.net/SUBnz/1/