宜昌本地软件开发与弱电工程服务商
项目咨询 · 快速响应
您当前的位置: > 博客2 > AS2与AS3技术

[AS3]as3多个setTimeout之间如何按顺序依次执行

2016-10-13 00:16:48 点击量:12 标签: 收藏本文

做了一个试验,测试能否在另外的setTimeout中中断其他代码的执行。结果表明,是不可能的,Actionscript会按顺序,逐个逐个的执行。

  1. private var index:int; 
  2. private var stop:Boolean = false
  3.  
  4. public function EffectTest() 
  5.        setTimeout(run, 200); 
  6.        setTimeout( function():void 
  7.        { 
  8.                trace("try to terminate running"); 
  9.               stop = true
  10.        }, 500); 
  11.        setTimeout( function():void 
  12.        { 
  13.                trace("index is", index); 
  14.        }, 600); 
  15.  
  16. protected function run(): void 
  17.         //this loop needs 1.3s to finish 
  18.         for (var i:int = 0; i < 10000000; i++) 
  19.        { 
  20.                if (stop) 
  21.               { 
  22.                      iindex = i; 
  23.                       trace("running has been terminated"); 
  24.                       return; 
  25.               } 
  26.                var a:Number = Math.sqrt(i); 
  27.        } 
  28.        iindex = i; 
  29.         trace("finish running"); 

如果第二个setTimeout能中止的话,应该输出running has been terminated

但实际运行结果,这部分是没有执行的。run函数还是好好的完全被执行完。
最终输出:
finish running
try to terminate running
index is 10000000

ABOUT US
宜昌农人网络科技有限公司

我们是一家提供综合软件外包与弱电工程服务的技术公司。

业务涵盖定制软件、管理系统、网站、小程序、APP、系统集成、网络布线、监控安防、门禁、机房建设及长期技术维护。

公司地址:湖北省宜昌市伍家岗区东站二路6号三峡(宜昌)大数据产业园海洋馆二楼A203室