Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 920
DexCurl Avatar asked Sep 02 '11 20:09

DexCurl


People also ask

Can I use querySelector in jQuery?

querySelector() and querySelectorAll() are two jQuery functions which helps the HTML elements to be passed as a parameter by using CSS selectors ('id', 'class') can be selected. querySelector() Method: The querySelector() method returns the first element within the document which matches a specified CSS selector(s).

Which selector is faster in jQuery?

ID and Element selector are the fastest selectors in jQuery.

What does $() mean in jQuery?

In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery.

What does the jQuery (' P ') line of code do?

<p>This is a paragraph selected by a jQuery method. </p> <p>This is also a paragraph selected by a jQuery method. </p> $("p").


4 Answers

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/

like image 175
jAndy Avatar answered Oct 04 '22 07:10

jAndy


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

$("#div").show().delay(5000).queue(function (next) {
    $(this).hide();
    next();
});
like image 42
gilly3 Avatar answered Oct 04 '22 06:10

gilly3


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

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

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

like image 45
Paul Avatar answered Oct 04 '22 07:10

Paul


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

.delay() works for animations only

like image 33
genesis Avatar answered Oct 04 '22 06:10

genesis