/// <summary> /// 停止计时器 /// </summary> /// <param name="timerKey">Timer key.</param> public static void StopTimer(string timerKey) { if (!mTimerTaskMap.ContainsKey(timerKey)) { return; } TimerTask timerItem = mTimerTaskMap[timerKey]; if (timerItem != null) { timerItem.Stop(true); } }
/// <summary> /// 移除计时器 /// </summary> /// <param name="timeKey"></param> public static void UnRegister(string timeKey) { if (!mTimerTaskMap.ContainsKey(timeKey)) { return; } TimerTask timerTask = mTimerTaskMap[timeKey]; if (timerTask != null) { timerTask.Stop(true); timerTask = null; mTimerTaskMap.Remove(timeKey); } }
/// <summary> /// 注册计时器 /// </summary> /// <param name="taskKey">计时器Key</param> /// <param name="callback">回调</param> /// <param name="totalTime">总时间,-1代表一直计时(毫秒)</param> /// <param name="intervalTime">间隔时间(毫秒)</param> /// <param name="endCallback">计时结束回调</param> public static void Register(string taskKey, Action <long> callback, long totalTime = -1, int delayTime = 0, float intervalTime = 1, Action endCallback = null) { TimerTask timerTask = null; if (!mTimerTaskMap.ContainsKey(taskKey)) { timerTask = new TimerTask(); mTimerTaskMap.Add(taskKey, timerTask); } else { timerTask = mTimerTaskMap[taskKey]; } if (timerTask != null) { timerTask.RunLogic(totalTime, intervalTime, delayTime, callback, endCallback); } }