ActionScript事件处理函数的执行顺序 [英] ActionScript event handler execution order

查看:227
本文介绍了ActionScript事件处理函数的执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图理解ActionScript中的事件实现的方式,但我坚持。

I have been trying to understand the way ActionScript's events are implemented, but I'm stuck.

我知道,AS是单线程的,这意味着只有一个事件处理程序将被执行的时间,并且也意味着处理程序将在一确定的顺序来执行*

I know that AS is single threaded, which means that only one event handler will be executing at a time, and also means that handlers will be executed in a deterministic order*.

例如,请考虑以下code:

For example, consider the following code:

1: var x = {executed: false};
2: foo.addEventListener("execute", function(){ x.executed = true; });
3: foo.dispatchEvent(new Event("execute"));
4: assert(x.executed);

如果动作是多线程的,这将是可能的,第4行断言可能有时会失败,成功等。

If ActionScript was multi-threaded, it would be possible that the assertion on line 4 could fail sometimes and succeed others.

不过,自是不是多线程的,它按理说,断言要么总是的fail²或总是的succeed³。或者,换言之,事件将被以确定的方式处理。

But since AS is not multi-threaded, it stands to reason that the assertion will either always fail² or always succeed³. Or, in other words, events will be handled in a deterministic way.

那么,这个假设(即事件处理野兔确定性)是否正确? Adobe是否提供任何明确的文档,对此事?

So, is this assumption (that events hare handled deterministically) correct? Does Adobe provide any definitive documentation on the matter?

注:我的只有的这里关注的是由则dispatchEvent 调度的事件 - 我认识到,外部调度事件(网络流量,用户输入,计时器滴答等)的行为不同。

Note: I am only concerned here with events dispatched by dispatchEvent – I realize that "externally dispatched" events (network traffic, user input, timers ticking, etc) behave differently.


*:除,当然,对于由像用户输入或网络流量不确定的东西触发的事件
²:推新事件到堆栈,然后连续弹出顶部事件从堆栈中,执行它,直到它结束,然后继续到下一个事件:如果,例如,如果事件处理算法是它总是失败。
³:如果由则dispatchEvent 调度的事件,尽快为他们被派到了处理,将总是成功

*: with the exception, of course, for events triggered by nondeterministic things like user input or network traffic.
²: it would always fail if, for example, if the event handling algorithm was: "push new events onto a stack, then continuously pop the top event off the stack, execute it until it terminates, then go on to the next event".
³: it would always succeed if events dispatched by dispatchEvent were handled as soon as they were dispatched.

推荐答案

除非我误解 - 在这种情况下,我道歉! - 我不同意与其他:你只需要测试您所提交看到x.executed真正每次跟踪值code

Unless I'm misunderstanding -- in which case I do apologize! -- I have to disagree with the others: you need only test the code you've submitted to see the value of x.executed traces true every time.

例如,如果在地方的Foo对象,你要替换IEventDispatcher,请(在这种情况下,我这样做的含蓄,用我的应用程序对象和其creationComplete处理函数),你会看到:

For example, if, in place of your foo object, you were to substitute an IEventDispatcher (in this case I do so implicitly, with my app object and its creationComplete handler), you'd see:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onCreationComplete()">

    <mx:Script>
    	<![CDATA[

    		private function onCreationComplete():void
    		{
    			var x = {executed: false};
    			addEventListener("execute", function() { x.executed = true; });
    			trace(x.executed); // false
    			dispatchEvent(new Event("execute"));
    			trace(x.executed); // true
    		}

    	]]>
    </mx:Script>

</mx:WindowedApplication>

当然,也有控制事件处理的的(使用优先级的addEventListener的参数)的方式,和事件传播的各个阶段的显示列表中的对象(例如,捕捉,瞄准,冒泡 - 请参阅Flex文档的详细信息,请这里这里),但在这种情况下,事件确实处理基本上是内联和优先顺序。根据文档:

Of course, there are ways of controlling event-handling order (using the priority argument of addEventListener), and various phases of event propagation for objects on the display list (e.g., capturing, targeting, bubbling -- see the Flex docs for detailed information, here and here), but in this kind of situation, events are indeed handled essentially inline and in priority order. According to the docs:

Flex的注册事件监听器   顺序调用addEventListener()   方法被调用。 Flex又调用   侦听功能当事件   发生在其中它们是顺序   注册。但是,如果你注册   一些事件侦听器内联和一些   与addEventListener()方法,   其中侦听器的顺序   要求一个单一事件可以是   联合国predictable。

Flex registers event listeners in the order in which the addEventListener() methods are called. Flex then calls the listener functions when the event occurs in the order in which they were registered. However, if you register some event listeners inline and some with the addEventListener() method, the order in which the listeners are called for a single event can be unpredictable.

希望帮助!

这篇关于ActionScript事件处理函数的执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆