20

I have div block which comes on a mouseover of another div.

div1 img // mouse over shows div2.

I want to show div2 for 10 seconds and then hide it , can you please tell me how to achive this

Thanks

kobe
  • 15,671
  • 15
  • 64
  • 91
  • Post a sample of HTML to get the best answer. There could be event-order issues if the divs are nested. – Silkster Nov 08 '10 at 20:13
  • @silkster , next time i will definetly do that. I got the answer , thanks very much – kobe Nov 08 '10 at 20:26
  • Possible duplicate of [jQuery show for 5 seconds then hide](http://stackoverflow.com/questions/3428766/jquery-show-for-5-seconds-then-hide) – Rick Smith Oct 28 '15 at 20:26

6 Answers6

35
$("#div1").mouseenter(function() {
    var $div2 = $("#div2");
    if ($div2.is(":visible")) { return; }
    $div2.show();
    setTimeout(function() {
        $div2.hide();
    }, 10000);
});

Another way to go:

$("#div1").mouseenter(function() {
    var $div2 = $("#div2");
    if ($div2.data("active")) { return; }
    $div2.show().data("active", true);
    setTimeout(function() {
        $div2.hide().data("active", false);
    }, 10000);
});
Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
  • @sime one more thing , if it is visible it should hide after 10 seconds..we shouldn't return there right. IF it is visible we hve to set t.he setTimout function on that – kobe Nov 08 '10 at 20:19
  • @gov Is the #div2 hidden in the beginning? – Šime Vidas Nov 08 '10 at 20:23
  • @sime , the div2 will be hidden onload of the page. – kobe Nov 08 '10 at 20:26
  • @gov When you hover #div1 for the first time, #div2 will be hidden, and the function will not return. Instead the #div2 will be shown and the timeout will be set. Now, if you hover again, and #div2 is still visible (the 10 seconds haven't passed yet), the function will return - because we don't want to set yet another timeout. – Šime Vidas Nov 08 '10 at 20:29
  • 1
    +1 I think this is the only answer that safeguards against multiple timeouts being set. – user113716 Nov 08 '10 at 20:41
20

Use jQuery's delay(n); method http://api.jquery.com/delay/

 $(function() {
      $("#div1 img").hover(function(){
        $("#div2").show().delay( 10000 ).hide(0);
      });
    });
Ram Patra
  • 16,266
  • 13
  • 66
  • 81
FatherStorm
  • 7,133
  • 1
  • 21
  • 27
  • 6
    There are two mistakes in your code: 1. the hover() method, when supplied with only one function argument, executes that function on both mouseenter **and mouseleave**, which is clearly not what the OP wants, 2. the delay() method does only work with animations and not with show() or hide() (Note: if you supply an argument to show() or hide(), it becomes an animation and then it does work). Your code `show().delay(10000).hide()` will hide the element **immediately** and not after 10 seconds. – Šime Vidas Nov 08 '10 at 23:26
  • 1
    See [answer by @user113716](http://stackoverflow.com/a/4128518/616644) for example of correct usage. – Rick Smith Oct 28 '15 at 20:25
13

The accepted answer is the only good one here.

I'm leaving an answer because most of the others fail for various reasons.

If you want to use .delay(), the item delayed needs to be part of the queue. The .hide() method is not. But if you give .hide() a duration, it is.

So you can do this:

var $div2 = $('#div2');

$('#div1').mouseenter(function() {
    $div2.show().delay( 10000 ).hide( 0 );
});

The 0 duration makes .hide() part of the queue. You don't want to use .hover() because it will fire once for mouseenter and once for mouseleave. This is not what was wanted.

Some of the answers using setTimeout() fail because if there are several mouseenter events, then several setTimeout() calls are made. The accepted answer gets around this.

user113716
  • 318,772
  • 63
  • 451
  • 440
8

as part of the mouseover function:

setTimeout(function(d){
  item.hide();
}, 10000);

This assumes var item is the jquery object of the div you want to hide. The parameter 10000 is the delay in milliseconds. 10s * 1000ms/1s = 10000ms

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • I voted for this one because even though this question is tagged as [jquery] it does not ask for a jQuery solution specifically. – Wally Lawless Nov 08 '10 at 20:15
3
$(function() {
    $("div1 img").hover(
        function() { //mouse over show div
            $("div2").show(); //.delay(10000).fadeOut();
        },
        function() { // mouse out, start timer to hide
            //$("div2").delay(10000).fadeOut();
        }
    );    
});
hunter
  • 62,308
  • 19
  • 113
  • 113
2
var $div2 = $('#div2');


$('#div1').mouseover( function(){
  $div2.show();

  setTimeout( function(){
    $div2.hide();
  }, 1000*10);

});
Harmen
  • 22,092
  • 4
  • 54
  • 76