tags:

views:

92

answers:

1

I'm using .show to display a hidden message after a successful form submit.

My question: How to display the message for 5 seconds then hide.

Thanks in advance.

+8  A: 

You can use .delay() before an animation, like this:

$("#myElem").show().delay(5000).fadeOut();

If it's not an animation, use setTimeout() directly, like this:

$("#myElem").show();
setTimeout(function() { $("#myElem").hide(); }, 5000);

You do the second because .hide() wouldn't normally be on the animation (fx) queue without a duration, it's just an instant effect.

Or, another option is to use .delay() and .queue() yourself, like this:

$("#myElem").show().delay(5000).queue(function(n) {
  $(this).hide(); n();
});
Nick Craver
Thanks, very clean and useful.
josoroma
@josoroma: If this answer was useful, then it's polite to tick it as "accepted", for the benefit of both the answerer and other people who may read this question in the future.
GlenCrawford