Apache flex Flex如何使用callLater?

Apache flex Flex如何使用callLater?,apache-flex,Apache Flex,在我的flex mobile应用程序中,我有一个循环运行了100多次。在每次迭代中,我都会更新特定标签的一些属性。由于循环非常耗时,我需要在每次迭代时更新屏幕并显示中间结果。如何打破循环并刷新显示列表 function theFunction():void{ for var i:int = 0; i < n; i++{ doBusyStuff(); label_1.text = "iteration"+" i"; } } function theFunction():void

在我的flex mobile应用程序中,我有一个循环运行了100多次。在每次迭代中,我都会更新特定标签的一些属性。由于循环非常耗时,我需要在每次迭代时更新屏幕并显示中间结果。如何打破循环并刷新显示列表

function theFunction():void{
 for var i:int = 0; i < n; i++{
  doBusyStuff();
  label_1.text = "iteration"+" i";
 }
}
function theFunction():void{
对于变量i:int=0;i
有几种方法可以强制重新绘制组件:

invalidateDisplayList();
invalidateProperties();
invalidateSize();
只需在函数中使用组件所需的任何内容,并在脚本之后使用
callLater(yourRedrawFunction)调用它即可

编辑:例如,在您的情况下:

function theFunction():void{
 for var i:int = 0; i < n; i++{
  doBusyStuff();
  label_1.text = "iteration"+" i";
 }
 callLater(yourRedrawFunction);
}
function theFunction():void{
对于变量i:int=0;i
在这种情况下,我更喜欢使用
flash.utils.setTimeout()


但是,
setTimeout()
callLater()
都取决于滴答声或帧速率,这意味着它们不会尽可能快。因此,如果您还想让它运行得更快,您应该让它在每次调用时运行几个循环。

另一个解决方案类似于Chaniks的回答,使用
DateTime
检查循环在每次迭代中运行的时间。一旦它检测到它已经运行了太长时间,它将结束循环并在下一帧再次拾取

var i:int;
function callingFunction():void {
  i = 0;
  stage.addEventListener(Event.ENTER_FRAME, theFunction);
}

function theFunction(e:Event):void {
  var time:DateTime = new DateTime();
  var allowableTime:int = 30;  //Allow 30ms per frame
  while ((new DateTime().time - time.time < allowableTime) && i < n) {
    doBusyStuff();
    i++;
  }
  if (i >= n) {
    stage.removeEventListener(Event.ENTER_FRAME, theFunction);
  }
  label_1.text = "iteration"+" i";
}
vari:int;
函数调用函数():void{
i=0;
stage.addEventListener(Event.ENTER_FRAME,函数);
}
函数函数(e:事件):无效{
var-time:DateTime=new-DateTime();
var allowableTime:int=30;//允许每帧30毫秒
while((new DateTime().time-time.time=n){
stage.removeEventListener(Event.ENTER_FRAME,函数);
}
label_1.text=“迭代”+“i”;
}
我必须在哪里呼叫“callLater”?在另一个功能中?如果你能用我上面的例子,我将不胜感激。当做
var i:int;
function callingFunction():void {
  i = 0;
  stage.addEventListener(Event.ENTER_FRAME, theFunction);
}

function theFunction(e:Event):void {
  var time:DateTime = new DateTime();
  var allowableTime:int = 30;  //Allow 30ms per frame
  while ((new DateTime().time - time.time < allowableTime) && i < n) {
    doBusyStuff();
    i++;
  }
  if (i >= n) {
    stage.removeEventListener(Event.ENTER_FRAME, theFunction);
  }
  label_1.text = "iteration"+" i";
}