热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

什么可能导致事件处理关闭停止工作?-Whatcancauseevent-handlingclosurestostopworking?

Illtrytobeasconciseaspossible.Ihaveanumberofobjectsinanarray,andImapplyingeven

I'll try to be as concise as possible. I have a number of objects in an array, and I'm applying event listeners to each one using closures:

我会尽量简洁。我在数组中有许多对象,我正在使用闭包为每个对象应用事件监听器:

//reduced to the logic in question:
buttons.forEach(function(button:EventDispatcher, i:int, list:Array):void {
  button.addEventListener(MouseEvent.MOUSE_OVER, function(e:Event):void {
    button.filters = [button_glow_filter];
  });
});
//button-specific click handlers:
buttons[0].addEventListener(MouseEvent.MOUSE_CLICK, handle_some_action);

This works perfectly for a while, until I perform an unrelated action on the UI. It's a very complex system, so I'm not really sure what is happening. I can confirm that the unrelated action has no direct effect on the object that contains the buttons or the buttons themselves (at least, it's not changing anything via the public interfaces). The buttons still exist, and the click event listeners still work correctly because those are individually assigned real functions on the class's interface.

这在一段时间内完美运行,直到我在UI上执行不相关的操作。这是一个非常复杂的系统,所以我不确定发生了什么。我可以确认不相关的动作对包含按钮或按钮本身的对象没有直接影响(至少,它不会通过公共接口改变任何东西)。按钮仍然存在,并且单击事件侦听器仍然可以正常工作,因为这些按钮在类的接口上单独分配了实际功能。

My question therefore is: does anyone know what can cause these closures to stop handling the MouseOver events without having any other perceptible effect on the related objects?

因此我的问题是:有没有人知道什么可以导致这些闭包停止处理MouseOver事件而不会对相关对象产生任何其他可察觉的影响?

There are a number of ways to accomplish this MouseOver behavior, and for now I've switched to one that works, but I'd still like to know the answer to this question for future reference.

有很多方法可以实现这种MouseOver行为,现在我已经切换到一个有效的方法,但我仍然想知道这个问题的答案以供将来参考。

1 个解决方案

#1


1  

I figured out the likely culprit almost immediately after posting: garbage collection. It took just a couple of minutes to confirm. This is exactly what the useWeakReference parameter is for in the addEventListener interface; it defaults to true. By setting it to false, it prevents listeners assigned in this fashion from being garbage collected. The correct code is:

在发布之后我几乎立即想出了可能的罪魁祸首:垃圾收集。只用了几分钟就确认了。这正是addEventListener接口中useWeakReference参数的用途;它默认为true。通过将其设置为false,可以防止以这种方式分配的侦听器被垃圾回收。正确的代码是:

buttons.forEach(function(button:EventDispatcher, i:int, list:Array):void {
  button.addEventListener(MouseEvent.MOUSE_OVER, function(e:Event):void {
    button.filters = [button_glow_filter];
  }, false, 0, false);
});

推荐阅读
author-avatar
Seet琸琸
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有