代码:
package Common.Utils
{
public class DelayRefreshQueue
{
private static var _queue:Object=new Object();//"uiclassname" : [{"caller":*,"method":funciton,"args"=[a,b...]},...]
public function DelayRefreshQueue()
{
}
public static function addRefreshTask(uiname:String,caller:*,method:Function,args:Array):void
{
if(_queue[uiname])
{
if(!checkLastRefreshIsSame(uiname,caller,method,args))
_queue[uiname].push({caller:caller,method:method,args:args});
}else
{
_queue[uiname] = new Array();
_queue[uiname].push({caller:caller,method:method,args:args});
}
}
public static function doRefreshTask(uiname:String,caller:*):void
{
if(_queue[uiname])
{
for(var i:int=0;i<_queue[uiname].length;i++)
{
if(_queue[uiname][i].caller == caller)
{
_queue[uiname][i].method.apply(_queue[uiname][i].caller,_queue[uiname][i].args);
(_queue[uiname] as Array).splice(i,1);
i--;
}
}
if((_queue[uiname] as Array).length==0)
{
//删除
_queue[uiname] = null;
delete _queue[uiname];
}
}
}
//检测上一次是否跟这次一样
private static function checkLastRefreshIsSame(uiname:String,caller:*,method:Function,args:Array):Boolean
{
if( _queue[uiname])
{
var last:Array = _queue[uiname] as Array;
var obj:Object = last[last.length-1];
if(obj)
{
if(obj.caller == caller && obj.method == method)
{
if(args==null&&obj.args==null)
{
return true;
}else
{
for(var i:int=0;i<obj.args.length;i++)
{
if(!args[i])
{
return false;
}else if(obj.args[i]!=args[i])
{
return false;
}
}
return true;
}
}
}
}
return false;
}
}
}
使用示例
//构造函数中定义:
DelayRefreshQueue.doRefreshTask("TacticalDlg",this);
//延迟某个方法到队列中
DelayRefreshQueue.addRefreshTask("TacticalDlg",this,refreshUI,null);
使用拓展
1、设置一个资源加载/创建是否完成的变量
2、如果资源加载/创建未完成,就使用addRefreshTask将某个方法/函数延迟到队列中