您的位置:首页 > 其它

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

2012-12-14 14:49 369 查看
在使用moduleloader加载module模块时,如果module模块中使用timer来控制执行某函数,当module被卸载后,timer控制执行的函数仍在执行。如:主应用程序为index.mxml(包含加载module的moduleloader控件),被加载的module为mo_test.swf(mo_test.mxml)在mo_test.mxml中使用timermo_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时也有些类似的解决方案。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐