定时器与 setTimeout [英] Timer vs setTimeout
问题描述
flash.utils.setTimeout() 状态的文档:
The docs for flash.utils.setTimeout() state:
不要使用这种方法,请考虑创建一个 Timer 对象,使用指定的间隔,使用 1 作为repeatCount 参数(设置定时器只运行一次).
Instead of using this method, consider creating a Timer object, with the specified interval, using 1 as the repeatCount parameter (which sets the timer to run only once).
有谁知道这样做是否有(显着)优势?当您只需要延迟 1 个调用时,使用 setTimeout 会容易得多.
Does anyone know if there is a (significant) advantage in doing so? Using setTimeout is a lot easier when you only need to delay 1 call.
推荐答案
setTimeout
实际上使用了一个 Timer
子类,SetIntervalTimer
,它是一个内部类.您可以通过执行 setTimeout(function ():void { throw "booom"; }, 1);
来检查.您将在堆栈跟踪中看到它.
setTimeout
actually uses a Timer
subclass, the SetIntervalTimer
, which is an internal class. You can check by doing setTimeout(function ():void { throw "booom"; }, 1);
. You'll see it in the stack trace.
因此,我真的看不出有什么大的缺点.唯一的区别是,您有 2 个匿名电话而不是一个.OTOH,在性能关键的情况下,您不应该使用任何一个(一个内部计时器除外)以避免 TimerEvent
对象的频繁实例化.
As such, I cannot really see a big disadvantage. The only difference is, that you have 2 anonymous calls instead of one. OTOH, in performance critical situations, you shouldn't be using either (except one internal timer) to avoid frequent instantiation of TimerEvent
objects.
基本上,我认为这是一个品味问题.Adobe 决定,AS3 事件系统是令人兴奋的,所以他们推广了它.
Basically, I think it's a matter of taste. Adobe decided, the AS3 event system is the shizzle, so they promote it.
这篇关于定时器与 setTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!