ActionScript:事件处理程序何时执行?
在 ActionScript 中,何时调度事件:
foo.addEventListener("some event", someHandler);
foo.dispatchEvent(new Event("some event"));
事件处理程序在什么时候执行?
我问是因为我在 Adobe 开发人员指南:
请注意,在调用远程服务后,某些属性会分配给 [AsyncToken]。 在多线程语言中,会存在竞争条件,即在分配令牌之前返回结果。 这种情况在 ActionScript 中不是问题,因为在当前执行的代码完成之前无法启动远程调用。
但我找不到任何有关“当前正在执行的代码”含义的信息。
When, in ActionScript, an event is dispatched:
foo.addEventListener("some event", someHandler);
foo.dispatchEvent(new Event("some event"));
At what point are the event handlers executed?
I ask because I caught this at the end of an Adobe developer guide:
Notice that some properties are assigned to the [AsyncToken] after the call to the remote service is made. In a multi-threaded language, there would be a race condition where the result comes back before the token is assigned. This situation is not a problem in ActionScript because the remote call cannot be initiated until the currently executing code finishes.
But I could not find any information on what they meant by "currently executing code".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在 ActionScript 中调用
dispatchEvent()
,处理程序将立即执行。 顺序首先由调用addEventListener()
时指定的优先级决定,如果优先级相同,则由添加它们的顺序决定。 先到先得。如果事件是从 Flash Player 内部调度的,例如来自
URLLoader
实例的Event.COMPLETE
或任何其他需要网络通信的事件,则在 ActionScript 运行时不会调度该事件。 它会排队等待稍后。 我想这正是为了避免文档中描述的竞争条件。 我相信已经观察到“稍后”是下一帧,但它可能在当前帧的所有其他 ActionScript 运行之后发生。If you call
dispatchEvent()
in ActionScript, the handlers will execute immediately. The order is determined first by the priority specified when you calladdEventListener()
and then by the order in which they were added if the priorities are the same. First come, first served.If an event is dispatched internally from Flash Player, such as
Event.COMPLETE
from aURLLoader
instance or anything else that requires network communication, it will not be dispatched while ActionScript is running. It gets queued up for later. I imagine this is precisely to avoid the race condition described in the documentation. I believe it has been observed that "later" is the next frame, but it could happen after all the other ActionScript for the current frame has run.Actionscript 是一种单线程事件驱动语言。 请注意 Actionscript 中没有“主要”方法。 所有代码都属于事件,例如。 初始化代码往往是为了响应“creationComplete”事件而放置的。 一旦运行该事件处理程序中的代码,就会执行下一个事件。 因此,如果您这样做:
其他处理程序将无法运行,因为当前正在执行的代码(无限循环)永远不会完成。
请注意,Flash 可能在内部使用多个线程,但这是从开发人员那里抽象出来的。
Actionscript is a single threaded event driven language. Notice how there are no "main" methods in Actionscript. All code belongs in events, eg. initialization code tends to be placed in response to "creationComplete" events. Once the code in that event handler is run, the next event is executed. So if you did:
No other handler would be able to run because the currently executing code (the infinite loop) would never complete.
Note that Flash probably uses multiple threads internally, but that is abstracted from the developer.