Jquery $('#div').show().delay(5000).hide(); doesn't work

JavascriptJqueryCss

Javascript Problem Overview


I'm trying to show a div thats set to display: none; for 5 seconds with

$('#div').show().delay(5000).hide();

but it deson't work, it just goes straight to hide()

Can any of you help me?

Javascript Solutions


Solution 1 - Javascript

Do it like this:

$('#div').show(0).delay(5000).hide(0);

By passing in numbers to .show() and .hide(), jQuery will take those methods into its internal fx queue (even if the number is zero). Since .delay() only works within a queue, you need that little workaround.

example: http://jsfiddle.net/zceKN/

Solution 2 - Javascript

You need to use .queue() because .hide() isn't queued by default.

$("#div").show().delay(5000).queue(function (next) {
    $(this).hide();
    next();
});

Solution 3 - Javascript

You need a duration on your hide for it to work:

$('#div').show('slow').delay(5000).hide('slow');

Example: http://jsfiddle.net/Paulpro/GLTaB/

Solution 4 - Javascript

$('#div').show();
setTimeout(function(){$('#div').hide();}, 5000);

.delay() works for animations only

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDexCurlView Question on Stackoverflow
Solution 1 - JavascriptjAndyView Answer on Stackoverflow
Solution 2 - Javascriptgilly3View Answer on Stackoverflow
Solution 3 - JavascriptPaulView Answer on Stackoverflow
Solution 4 - JavascriptgenesisView Answer on Stackoverflow