Flash actionscript3.0 多个setTimeout之间会顺序执行 单线程执行 无法中止
做了一个试验,测试能否在另外的setTimeout中中断其他代码的执行。结果表明,是不可能的,Actionscript会按顺序,逐个逐个的执行。
如果第二个setTimeout能中止的话,应该输出running has been terminated
但实际运行结果,这部分是没有执行的。run函数还是好好的完全被执行完。
最终输出:
finish running
try to terminate running
index is 10000000
private var index:int; private var stop:Boolean = false; public function EffectTest() { setTimeout(run, 200); setTimeout( function():void { trace("try to terminate running"); stop = true; }, 500); setTimeout( function():void { trace("index is", index); }, 600); } protected function run(): void { //this loop needs 1.3s to finish for (var i:int = 0; i < 10000000; i++) { if (stop) { index = i; trace("running has been terminated"); return; } var a:Number = Math.sqrt(i); } index = i; trace("finish running"); }
如果第二个setTimeout能中止的话,应该输出running has been terminated
但实际运行结果,这部分是没有执行的。run函数还是好好的完全被执行完。
最终输出:
finish running
try to terminate running
index is 10000000
kenkofox@qq.com
https://github.com/kenkozheng
欢迎投简历给我,一线大厂工作机会