86

How can I use delay() with show() and hide() in Jquery ?

yAnTar
  • 4,269
  • 9
  • 47
  • 73
faressoft
  • 19,053
  • 44
  • 104
  • 146

5 Answers5

180

Pass a duration to show() and hide():

When a duration is provided, .show() becomes an animation method.

E.g. element.delay(1000).show(0)

DEMO

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
7

The easiest way is to make a "fake show" by using jquery.

element.delay(1000).fadeIn(0); // This will work
5

Why don't you try the fadeIn() instead of using a show() with delay(). I think what you are trying to do can be done with this. Here is the jQuery code for fadeIn and FadeOut() which also has inbuilt method for delaying the process.

$(document).ready(function(){
   $('element').click(function(){
      //effects take place in 3000ms
      $('element_to_hide').fadeOut(3000);
      $('element_to_show').fadeIn(3000);
   });
}
abhinav1602
  • 1,190
  • 17
  • 18
1

from jquery api

Added to jQuery in version 1.4, the .delay() method allows us to delay the execution of functions that follow it in the queue. It can be used with the standard effects queue or with a custom queue. Only subsequent events in a queue are delayed; for example this will not delay the no-arguments forms of .show() or .hide() which do not use the effects queue.

http://api.jquery.com/delay/

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
0

This info needs updating.

I was looking at this today because I needed to delay the showing of a div. I'm using jQuery 3.4.1 and have tested this.

 $("#mydiv").delay(5000).show(200); // a 5 second delay before the 200 microseconds animation effect from hidden to visible is triggered.
SJacks
  • 408
  • 3
  • 19
  • This answer is 100% correct and has been tested. It should not be voted down. – SJacks Feb 24 '22 at 19:15
  • I guess it's because people stop reading and try a `$("#mydiv").delay(5000).show();` which DOES NOT work. You have to set any parameter for show to get it to work (e.g. `$("#mydiv").delay(5000).show(0);`), see accepted solution below. – fseydel Jan 26 '23 at 08:11