setInterval /clearInterval 下拉问题

发布于 2024-09-29 07:16:56 字数 916 浏览 7 评论 0原文

我想使用 jquery 制作一个下拉菜单,但它甚至不应该关闭。

我有一个订阅了 mouseenter 事件的元素,鼠标进入该元素,下拉菜单,启动,一切似乎都正常。我转到项目列表,它是其中包含一些内容的列表元素。我第一次更改这两个元素时,一切都按预期进行。因此,我保留了调用 mouseout 事件的元素,并且在 1000 毫秒内列表不再可见。 我第二次打开“下拉菜单”似乎没问题,但如果我浏览列表,它就会像我离开元素时那样消失。在某种程度上,这是合乎逻辑的,因为两个元素(按钮和列表)中的每一个都订阅了两个事件:mouseenter 和 mouseleave。 mouseenter提供了clearInterval,mouseout提供了setInterval。数据存储在名为 tTimer 的变量中,该变量的默认值为 0。 我真的不知道为什么会发生这种情况。这是我的代码的一个片段:

        var tTimer = 0;
        var rmItemList = function(){itemList.remove(); clearTimeout(tTimer); }

        $(this).bind("mouseleave",function(){ tTimer = window.setTimeout(rmItemList, 1000); });
        itemList.bind("mouseleave",function(){ tTimer = window.setTimeout(rmItemList, 1000); });
        $(this).bind("mouseenter",function(){ window.clearTimeout(tTimer); itemList.appendTo("body"); });
        itemList.bind("mouseenter",function(){ window.clearTimeout(tTimer); });

I want to make a dropdown menu using jquery, but it closes even it shouldn't.

I have an element which is subscribed to the mouseenter event, mouse enters the element, the dropdown, fires up, everything seems to be ok. I go down to the item list which is list element with some content in it. The first time i change the two elements everything works as is expected. So i leave the elements the mouseout event is called and within 1000 ms the list isn't visible anymore.
The second time i open the "dropdown" it seems to be ok, but if i go over the list, it disappears like it should when i leave the elements. Some way its logical because every of the two elements (button and list) are subscribed to two events: mouseenter and mouseleave. Mouseenter provides a clearInterval, mouseout provides a setInterval. Data is stored in a variable called tTimer which has a default value from 0.
I really dont know why this happens. heres a snipplet from my code:

        var tTimer = 0;
        var rmItemList = function(){itemList.remove(); clearTimeout(tTimer); }

        $(this).bind("mouseleave",function(){ tTimer = window.setTimeout(rmItemList, 1000); });
        itemList.bind("mouseleave",function(){ tTimer = window.setTimeout(rmItemList, 1000); });
        $(this).bind("mouseenter",function(){ window.clearTimeout(tTimer); itemList.appendTo("body"); });
        itemList.bind("mouseenter",function(){ window.clearTimeout(tTimer); });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦明 2024-10-06 07:16:56

我发现想象你正在做的事情有点棘手。然而,我发现您不断地从 DOM 中添加和删除元素。我相信 .remove() 从元素中删除事件处理程序。使用 .show().hide() 可能会更好。

这将具有显着提高性能的额外优势。

I'm finding it a little tricky to visualise what you're doing. I see, however, that you are repeatedly adding and removing an element from the DOM. I believe that .remove() removes event handlers from the element. It might well be better to use .show() and .hide() instead.

This would have the additional advantage of much improved performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文