ActionScript:事件处理程序何时执行?

发布于 2024-07-24 16:26:27 字数 715 浏览 3 评论 0原文

在 ActionScript 中,何时调度事件:

foo.addEventListener("some event", someHandler);
foo.dispatchEvent(new Event("some event"));

事件处理程序在什么时候执行?

我问是因为我在 Adobe 开发人员指南

请注意,在调用远程服务后,某些属性会分配给 [AsyncToken]。 在多线程语言中,会存在竞争条件,即在分配令牌之前返回结果。 这种情况在 ActionScript 中不是问题,因为在当前执行的代码完成之前无法启动远程调用

但我找不到任何有关“当前正在执行的代码”含义的信息。


另请参阅: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".


See also: ActionScript event handler execution order

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

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

发布评论

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

评论(2

染柒℉ 2024-07-31 16:26:27

如果您在 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 call addEventListener() 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 a URLLoader 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.

奶气 2024-07-31 16:26:27

Actionscript 是一种单线程事件驱动语言。 请注意 Actionscript 中没有“主要”方法。 所有代码都属于事件,例如。 初始化代码往往是为了响应“creationComplete”事件而放置的。 一旦运行该事件处理程序中的代码,就会执行下一个事件。 因此,如果您这样做:

private function someOtherHandler():void 
{
    foo.addEventListener("some event", someHandler);
    while(true) { ... spin wheels ... }
}

其他处理程序将无法运行,因为当前正在执行的代码(无限循环)永远不会完成。

请注意,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:

private function someOtherHandler():void 
{
    foo.addEventListener("some event", someHandler);
    while(true) { ... spin wheels ... }
}

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.

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