English 中文(简体)
jquery hover and settimeout. 概述
原标题:jquery hover and setTimeout / clearTimeOut

I m currently trying to do a menu with submenu. Here is what i want to do.

On hover a link (#mylink) i want to display a div (lets call it "#submenu") right under it. On mouse leave this link a want to execute a function after 5 second.

In this interval of 5 seconds, if i hover my div (#submenu) i want to clearTimeout. So this div won t desapear after 5 seconds.

我的法典如下:

$(document).ready(function()
{
    $("#mylink").hover(
        function ()
        {
            $( #submenu ).show();
        },
        function()
        {
            var timer = setTimeout(function(){$( #submenu ).hide();}, 5000);
        }
    );

    $("#submenu").hover(
        function ()
        {
            clearTimeout(timer);
        },
        function()
        {
            $( #submenu ).show();
        }
    );
}
最佳回答

SLaks has the right answer, but to elaborate on it, you would put var timer outside the function handler. Note that this example doesn t make timer a global variable - it just widens its scope so all handlers can use it.

$(document).ready(function(){
    var timer;
    $("#mylink").hover(
        function(){
            $( #submenu ).show();
        }, function(){
            timer = setTimeout(function(){$( #submenu ).hide();}, 5000);
        }
    );

    $("#submenu").hover(
        function(){
            clearTimeout(timer);
        }, function(){
            $( #submenu ).show();
        }
    );
}
问题回答

var timer is a local variable.
It doesn t exist outside that handler.

你们需要使之成为一个全球性的变量。

在这方面,我将如何这样做。

var timer
$("#mylink").mouseenter(function(){clearTimeout(timer);$( #submenu ).show()})
$("#mylink").mouseout(function(){timer = setTimeout(function(){$( #submenu ).hide();}, 1000);})

如果你在肉体链条内放下子,你会赢得第二门子,需要第二场活动手。

var timer;
$(document).ready(function()
{
    $( #mylink ).hover(function()
    {
        clearTimeout(timer);
        $( #submenu ).show();
    },function()
    {
        timer = setTimeout(function(){$( #submenu ).hide();},5000);
    });
}

这样,你就不需要ery。 在便衣中,只有如此长的时间才能开脱。





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签