Javascript – How to pause before fading an element out using jQuery

animationjavascriptjquery

I would like to flash a success message on my page.

I am using the jQuery fadeOut method to fade and then remove the element. I can increase the duration to make it last longer, however this looks strange.

What I would like to happen is have the element be displayed for five seconds, then fade quickly, and finally be removed.

How can you animate this using jQuery?

Best Answer

The new delay() function in jQuery 1.4 should do the trick.

$('#foo').fadeIn(200).delay(5000).fadeOut(200).remove();