简体   繁体   English

jQuery代码导致内存泄漏

[英]jQuery code causing memory leak

The following code is causing a memory leak (you can see this happening the more you hover in and out the slower it gets). 以下代码导致内存泄漏(您将鼠标悬停在其中的次数越多,看到的越多)。 Unfortunately I am unable to download a javascript profiler in my office (I can, it will just take me a few days/ weeks). 不幸的是,我无法在我的办公室中下载javascript分析器(我可以,这将花费我几天/几周的时间)。

Here is the code, just some simple transitions for a dropdown menu: 这是代码,只是下拉菜单的一些简单转换:

$(document).ready(function(){
    breadcrumbOver = function () {
        $(this).stop().animate({ backgroundColor: "#3393b5", textIndent: 15 }, 250);
    }
    breadcrumbOut = function () {
        $(this).stop().animate({ backgroundColor: "#738793", textIndent: 0 }, 250);
    }
    $("nav ul li").hover(
      function () {
        $(this).children('ul.child').stop().slideDown('fast').children('li').hover(breadcrumbOver, breadcrumbOut);
      }, 
      function () {
        $(this).children('ul.child').stop().slideUp('fast').unbind(breadcrumbOver, breadcrumbOut);
      }
    );
});

Can anyone see where a memory leak may be occuring? 谁能看到可能发生内存泄漏的地方?

Edit: Live example here - http://rcnhca.org.uk/sandbox/ (Repeatedly roll over "Health, Safety and Security" then roll over it's children to see the effect happen, also the animation slideDown doesn't fire sometimes if you roll in and out fast enough). 编辑:活生生的例子在这里- http://rcnhca.org.uk/sandbox/ (在“健康,安全和保安”反复滚再滚了它的孩子看到效果发生,也动画了slideDown并不有时如果火您可以足够快地滚动进出)。

The problem looks like it might be in your initial selector. 问题看起来可能在您的初始选择器中。 It targets all li tags under a ul under nav , which includes all children and grandchildren. 它以navul下的所有li标签为目标,其中包括所有子孙。

$("nav ul li") ...

This causes it to add a hover callback to all li tags under nav , then when you hover it adds yet another hover callback. 这将导致它向nav下的所有li标签添加一个hover回调,然后在您悬停时添加另一个hover回调。

You might want to be more specific with it, such as specifically targeting the direct children. 您可能希望对其进行更具体的说明,例如专门针对直接孩子。

$("nav>ul>li")

If you have children classes, you can also use :not(.child) to target everything that's not a child. 如果您有子类,也可以使用:not(.child)定位所有:not(.child)类。 Use console.log (which is built into Chrome or using Firebug) to log what those selectors are pulling to make sure you're getting what you expect. 使用console.log (Chrome内置或使用Firebug)记录这些选择器要提取的内容,以确保您获得了期望的结果。

I see what you're talking about George; 我明白你在谈论乔治。 the effect is most noticeable when swiping between elements rapidly after the box has been opened several times. 打开盒子几次后在元素之间快速滑动时,效果最为明显。

Perhaps this will be something that impacts what you're seeing. 也许这会影响您所看到的东西。 I read up on stop() and felt like it could have an impact on what you're seeing. 我在stop()上阅读,觉得它可能会对您看到的内容产生影响。 The first attribute you could specify in stop() is clearQueue. 您可以在stop()指定的第一个属性是clearQueue。 The second is equally interesting. 第二个同样有趣。 Here is what the documentation says about it: 以下是文档说明的内容:

clearQueue A boolean indicating whether to remove queued animation as well. clearQueue一个布尔值,指示是否也要删除排队的动画。 Defaults to false. 默认为false。 jumpToEnd A Boolean indicating whether to complete the current animation immediately. jumpToEnd一个布尔值,指示是否立即完成当前动画。 Defaults to false. 默认为false。

Might be worth a shot to pass in true like this: 可能值得这样传递:

$(this).children('ul.child').stop(true, true)...
// (or  you want the animation to unwind, I suppose)
$(this).children('ul.child').stop(true)...

Also, I checked out unbind as well and I don't think you can pass in two events like you are. 另外,我也检查了取消绑定 ,并且我认为您不能像您一样传递两个事件。 Perhaps you could use it this way and not experience the problems you are: 也许您可以通过这种方式使用它,而不会遇到自己的问题:

...unbind(breadcrumbOver).unbind(breadcrumbOut);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM