-1

I'm at a bit of a loss here. I have an issue that I think may be due to mouse events taking precedence. I have a function f being invoked on mouse clicks - f does some work, then invokes another function g. Is it even possible that f runs, then another click happens - invoking f again - and then g is executed?

If my phrasing is hard to understand, I'll try to show what I think may be happening:

    click1 -----       /-----------\
                \     /             \
                  f --      f--      g    g
                           /   \         /
      click2 ------------ /     \--------

 |---------------- timeline----------------------|

I can say for certain the issue only arises (out of ~50 slow and ~50 quick double-clicks) when clicking twice in very quick succession (and not always even then). I realize my figure may confuse more than it clarifies, but I'm not sure how else to communicate my thoughts. Any input is greatly appreciated!

4

1 回答 1

2

AS3 是一个单线程代码执行环境,它将执行所有相关代码。如果单击触发了一系列方法的执行,则所有这些方法都将在任何其他代码再次执行之前运行。因此,由于 AS3 代码的单线程性质,因此在代码执行中不能存在竞争条件。

在这方面,AS3 中的所有事件都不是特例,当它们的侦听器触发时,其所有代码都以相同的方式执行,并且在完成之前无法执行其他代码。

特殊情况是:

  • 您可以使用计时器暂停执行,这样代码的执行将在以后发生。在这种情况下,无法保证这些计时器的触发将与其启动顺序同步。

  • 执行异步命令(比如加载一些东西),在这种情况下也不能保证加载操作也会按顺序发生。

但是这些特殊情况并没有违反 AS3 中的代码执行原则,所有代码都在一个线程中执行,因此它们不能有任何形式的重叠。

于 2016-01-15T14:36:17.233 回答