现在的位置: 首页 > 综合 > 正文

flex 4.0 解决在MODULE中使用timer实时刷新数据,卸载MODULE后还在执行的问题

2013年08月17日 ⁄ 综合 ⁄ 共 1051字 ⁄ 字号 评论关闭
在使用moduleloader加载module模块时,如果module模块中使用timer来控制执行某函数,当module被卸载后,timer控制执行的函数仍在执行。如:
主应用程序为index.mxml(包含加载module的moduleloader控件),被加载的module为mo_test.swf(mo_test.mxml)
在mo_test.mxml中使用timer
mo_test.mxml中:

某函数中{

timer.addEventListener("timer", timerHandler);
timer.start();

}


public function timerHandler(e:TimerEvent):void

{

执行代码A

}

解决方法:

在主应用程序中定义全局变量保存module的状态,即当前是否被加载。然后在timerHandler中调用判断
index.mxml中:

public var moduleState:Boolean=false;


.......


//并在加载module前将moduleState设置为true

this.moduleState=true;


.........

//在切换moduleloader中的module时或卸载module时将moduleState设置为false

this.moduleState=false;

mo_test.mxml中:


某函数中{

timer.addEventListener("timer", timerHandler);
timer.start();

}


public function timerHandler(e:TimerEvent):void

{

if(!this.parentApplication.moduleState)

{

timer.stop();

return;

}

执行代码A

}

到现在,当mo_test被卸载以后,timerHandler已经不再执行了。但当再次加载mo_test的时候有可能报错:

错误:无法将 Object@ce19af1 转换为 mx.messaging.messages.IMessage
具体是什么原因没仔细推敲。
解决方法:在index.mxml中添加代码如下:

import mx.messaging.messages.RemotingMessage;
private var rm:RemotingMessage;//只是定义一下就好了。很莫名奇妙的问题,在module中使用datagrid和tabnavigater时也有些类似的解决方案。

抱歉!评论已关闭.