zoukankan      html  css  js  c++  java
  • 时间事件管理工具类

    /*
    TimerManager.add(hs,1000,hs1);
    var a:int = 0;
    function hs():void
    {
    trace(a++);
    }

    function hs1():Boolean
    {
    if(a==5)
    {
    return true;
    }
    return false;
    }
    */

    package com.upupgame.utils
    {
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    /**
    * ******通过使用这个类,我们可以节省很多资源,所有频率相同的timer都将统一成一个timer来管理,
    * 这样可以节约很多性能,当然也可以定义不同频率的timer,但是在这里鼓励统一频率
    * 这样可以只需要一个timer就完成所有的循环工作,
    * 然后当使用这个管理类的时候不要担心会丢失常用的timer功能,
    * 例如你想在达到某个条件停止物体的循环运动的时候,
    * 你可以在添加的时候加一个限制函数,甚至比普通的用法更简单
    *
    * ******这个类同时提供一些其他的有用功能,
    * 其中尤为重要的是可以在任何时刻调用输出信息函数,
    * 可以查看当前使用了几个timer,以及每个timer的频率和绑定的函数列表,这样可以方便
    * 地在调试的时候查看信息
    *
    * ******这个类的使用非常简单,如果你不想了解其内部机制,
    * 则只需要在想添加一个timer的时候,
    * 使用一句TimerManager.add(..)就可以了,
    * 如果定时器没有启动,则调用此函数后将立即启动
    * 当满足了停止的条件或者手动移除了所有的函数列表的时候timer将自动停止,
    * 不用担心在不需要循环的时候会占用额外的cpu时间
    *
    * ******你可以在添加每个函数的时候添加一个限制函数,
    * 这个限制函数必须有返回值,如果返回值是false,则循环将一直进行下去,
    * 如果某个循环中限制函数的返回值为true,则停止此循环
    */
    public class TimerManager
    {
    /**
    *定时器列表,格式为timerList.T20:Timer.之所以如此结构是为了方便多个timer的取出和赋值,方便管理大量的timer对象
    */
    public static var timerList:Object=new Object();
    /**
    *函数列表,结构跟定时器列表类似,为functionList.T1:Array,里面包含了某个定时器的所有要执行的函数引用
    */
    private static var functionList:Object=new Object();
    /**
    *循环函数,每个间隔的Timer都有一个循环执行的函数, functionList.T1:Function
    */
    private static var funcList:Object=new Object();

    private static var limitFuncList:Object=new Object();
    /**
    *向管理器添加一个要定时执行的函数,
    * 第二个参数必须有返回值,当返回值为true的时候,将停止此绑定函数的循环
    * 如果没有指定第三个参数,默认为1秒执行一次
    * 执行此函数后,将自动开启timer,而无需手动开启,当满足条件后将根据条件移除某个函数,当函数列表为空时,
    * 将停止timer,节省资源,
    * @param func 要添加的函数
    * @param limitFunc 绑定的限制函数,当满足此函数(==true)时,将停止此循环,即移除此函数,
    * 如果要再满足条件时执行其他函数,请在第一个参数
    * 函数中书写,无需特殊绑定
    * @param timerInter 执行此函数的周期
    * @return
    *
    */

    public static function add(func:Function,limitFunc:Function=null,timerInter:int=1000):void
    {
    if (limitFunc == null)
    {
    limitFunc = function():Boolean
    {
    return false;
    }
    }
    var ii:String="T"+timerInter;
    if (timerList[ii] == undefined)
    {
    //如果还没有定义这个间隔的timer,则定义之
    timerList[ii]=new Timer(timerInter);
    }
    //向某个定时器添加一个要定时执行的函数
    if (functionList[ii] == undefined)
    {
    functionList[ii]=new Array();
    functionList[ii].push(func);

    limitFuncList[ii]=new Array();
    limitFuncList[ii].push(limitFunc);
    }
    else
    {
    functionList[ii].push(func);
    limitFuncList[ii].push(limitFunc);
    }

    if (funcList[ii] == undefined)
    {
    //定义某个定时器循环函数
    funcList[ii] = function():void
    {
    //满足条件时,移除此函数,停止对其的循环
    try
    {
    for (var i:int = 0; i < limitFuncList[ii].length; i++)
    {
    //trace(limitFuncList[ii][i]());
    if (limitFuncList[ii][i]() == true)
    {
    TimerManager.removeFunc(functionList[ii][i],timerInter);
    }
    }

    }
    catch (e:Error)
    {
    }
    var length:int=functionList[ii].length;
    if (length == 0)
    {
    timerList[ii].stop();
    }
    // trace(length)
    for ( i = 0; i < functionList[ii].length; i++)
    {
    //执行所有的函数
    // trace(functionList[ii])
    functionList[ii][i]();
    }
    }
    timerList[ii].addEventListener(TimerEvent.TIMER,funcList[ii]);
    }

    if (timerList[ii].running == false)
    {
    //如果当前定时器没有运行则运行之
    timerList[ii].start();
    }
    }
    /**
    *从某个频率的timer函数列表里移除某函数
    * @param func
    * @param timerInter
    *
    */
    public static function removeFunc(func:Function,timerInter:int=1000):void
    {
    var ii:String="T"+timerInter;
    //搜索数组,如果发现的确有此func,则删除之,否则不做任何处理,也不抛出任何错误
    var index:int=functionList[ii].indexOf(func);
    if (index >= 0)
    {
    //存在此函数
    functionList[ii].splice(index,1);
    limitFuncList[ii].splice(index,1);
    //trace(functionList[ii]);
    }
    }
    /**
    *在调试中输出当前所有定时器的详细信息
    * @return
    *
    */
    public static function traceInfo():void
    {
    trace("当前运行的计时器如下:")
    for (var timer:* in timerList)
    {
    trace("timer-"+timer+":时间间隔:"+timerList[timer].delay+"已运行次数:"+timerList[timer].currentCount+"运行状态:"+timerList[timer].running);

    trace("当前timer执行的函数列表如下:");
    for (var e:* in functionList[timer])
    {
    trace(" "+e+":"+getFunctionName(functionList[timer][e]));
    }
    }
    }
    /**
    *获取函数名,输入一个函数对象,将获取到此函数的函数名,用于输出信息
    * @param fun
    * @param useParameter
    * @return
    *
    */
    public static function getFunctionName(fun:Function):String
    {
    var funName:String = "";

    try
    {
    fun(fun, fun, fun, fun, fun);
    fun();
    }
    catch (err:Error)
    {
    funName = err.message.toString().replace(/.+#|().*|.+: /g, "");
    }
    if (funName == "")
    {
    return "只能获取类层次的函数名,无法获取局部函数名";
    }
    return funName;
    }
    public static function pauseTimer(timerInterval:int=1000):void
    {
    for (var i:* in timerList)
    {
    timerList[i].stop();
    }
    }
    public static function resumeTimer(timerInterval:int=1000):void
    {
    for (var i:* in timerList)
    {
    timerList[i].start();
    }
    }
    }
    }

  • 相关阅读:
    hdu 4027 Can you answer these queries? 线段树
    ZOJ1610 Count the Colors 线段树
    poj 2528 Mayor's posters 离散化 线段树
    hdu 1599 find the mincost route floyd求最小环
    POJ 2686 Traveling by Stagecoach 状压DP
    POJ 1990 MooFest 树状数组
    POJ 2955 Brackets 区间DP
    lightoj 1422 Halloween Costumes 区间DP
    模板 有源汇上下界最小流 loj117
    模板 有源汇上下界最大流 loj116
  • 原文地址:https://www.cnblogs.com/chenhongyu/p/3342046.html
Copyright © 2011-2022 走看看