一個實用的計時器,可以進行暫停,繼續,停止控制。
var Timer_State =<br />{<br />Start: 1,<br />Stop: 2,<br />Pause: 3<br />};<br />//expression 計時器要執行的指令碼字串<br />//interval 執行間隔 單位毫秒<br />//nMaxExeCount 執行次數 為 Number.POSITIVE_INFINITY 時不斷執行<br />//bFirstExe 為true時先執行運算式然後再進行下一次計時,為false時先執行下一次的計時再執行運算式<br />function Timer(expression, interval, nMaxExeCount, bFirstExe)<br />{<br />if (Timer._Initialized == undefined)<br />{<br />//開始<br />Timer.prototype.Start = function()<br />{<br />if (this.State == Timer_State.Start) return;<br />this.State = Timer_State.Start;<br />this.doStart();<br />}<br />Timer.prototype.doStart = function()<br />{<br />this.CreateExpressionTimer();<br />}<br />Timer.prototype.CreateExpressionTimer = function()<br />{<br />var exp = this.BuildExpression(this.Expression);<br />this._Timer = setTimeout(exp, this.Interval);<br />}<br />//運算式執行完畢<br />Timer.prototype.ExecuteComplete = function()<br />{<br />this.ExeCount++;<br />if (this.MaxExeCount != Number.POSITIVE_INFINITY && this.ExeCount >= this.MaxExeCount)<br />{<br />this.Stop();<br />return;<br />}<br />if (this.State == Timer_State.Start) this.CreateExpressionTimer();<br />}<br />//停止<br />Timer.prototype.Stop = function()<br />{<br />if (this.State == Timer_State.Stop) return;<br />this.State = Timer_State.Stop;<br />this.doStop();<br />}<br />Timer.prototype.doStop = function()<br />{<br />this.ClearCheckPause();<br />if (this._Timer == null) return;<br />clearTimeout(this._Timer);<br />this._Timer = null;<br />}<br />//暫停<br />Timer.prototype.Pause = function(fCheckPause, checkInterval)<br />{<br />if (fCheckPause == null || typeof (fCheckPause) != "function") {<br />throw "參數類型異常";<br />}<br />if (this.State == Timer_State.Stop) return;<br />this.ClearCheckPause();<br />this.State = Timer_State.Pause;<br />this.CheckPauseHandler = fCheckPause;<br />if (checkInterval == null) checkInterval = 500;<br />this.CheckInterval = checkInterval;<br />this.doStop();<br />this.doPause();<br />}<br />Timer.prototype.doPause = function()<br />{<br />this._CheckPauseTimer = setTimeout("Timer.CheckPause(" + this.Id + ");", this.CheckInterval);<br />}<br />Timer.prototype.CheckPause = function()<br />{<br />if (this.CheckPauseHandler == null) return;<br />if (this.CheckPauseHandler()) {<br />this.Resume();<br />}<br />else<br />{<br /> this.doPause();<br />}<br />}<br />Timer.prototype.ClearCheckPause = function()<br />{<br />if (this._CheckPauseTimer == null) return;<br />clearTimeout(this._CheckPauseTimer);<br />this._CheckPauseTimer = null;<br />this.CheckPauseHandler = null;<br />}<br />//恢複<br />Timer.prototype.Resume = function()<br />{<br />this.ClearCheckPause();<br />if (this.State == Timer_State.Stop) return;<br />this.Start();<br />}<br />Timer.prototype.BuildExpression = function(expression) {<br />var str = "Timer.ExecuteComplete(" + this.Id + ");";<br />var _expression = this.FirstExe ? (expression + ";" + str) : (str + expression);<br />return _expression;<br />}<br />//釋放資源<br />Timer.prototype.Dispose = function()<br />{<br />this.Stop();<br />this.Expression = null;<br />this.CheckPauseHandler = null;<br />Timer.Timers.Remove(this);<br />}<br />}<br />if (interval == null || expression == null)<br />{<br />throw "缺少參數";<br />}<br />if (typeof (expression) != "string")<br />{<br />throw "參數類型不正確";<br />}<br />if (nMaxExeCount == null) nMaxExeCount = Number.POSITIVE_INFINITY;<br />if (bFirstExe == null) bFirstExe = false;<br />this.MaxExeCount = nMaxExeCount;<br />this.ExeCount = 0;<br />this.FirstExe = bFirstExe;<br />this.Interval = interval;<br />this.State = Timer_State.Stop;<br />this._Timer = null;<br />this.CheckInterval = 500;<br />this.CheckPauseHandler = null;<br />this._CheckPauseTimer = null;<br />this.Id = Timer.GetNewId();<br />this.Expression = expression;<br />Timer.Timers.push(this);<br />}<br />//根據值取得索引<br />Array.prototype.IndexOf = function(oValue)<br />{<br />var count = this.length, value;<br />for (var i = 0; i < count; i++)<br />{<br />if (this[i] == oValue) {<br />return i;<br />}<br />}<br />return -1;<br />}<br />//根據索引移除<br />Array.prototype.RemoveByIndex = function(iStartIndex, iDeleteCount)<br />{<br />if (iDeleteCount == null) iDeleteCount = 1;<br />this.splice(iStartIndex, iDeleteCount);<br />return this.length;<br />}<br />//移除項<br />Array.prototype.Remove = function(oValue)<br />{<br />var index = this.IndexOf(oValue);<br />return this.RemoveByIndex(index);<br />}<br />Timer.Timers = new Array();<br />Timer.LastId = 0;<br />Timer.GetNewId = function()<br />{<br />++Timer.LastId;<br />return Timer.LastId;<br />}</p><p>Timer.GetTimerById = function(id)<br />{<br />var count = Timer.Timers.length;<br />for (var i = 0; i < count; i++)<br />{<br />var timer = Timer.Timers[i];<br />if (timer.Id == id)<br />{<br />return timer;<br />}<br />}<br />return null;<br />}<br />Timer.ExecuteComplete = function(id)<br />{<br />var timer = Timer.GetTimerById(id);<br />if (timer != null) timer.ExecuteComplete();<br />}<br />Timer.CheckPause = function(id)<br />{<br />var timer = Timer.GetTimerById(id);<br />if (timer != null) timer.CheckPause();<br />}
可以用如下方式使用:
function createTimer()<br />{<br />historyTimer = new Timer("playHistoryData();",intervalTime,Number.POSITIVE_INFINITY,true);<br />}<br />//start timer<br />function StartTimer()<br />{<br />//alert("start timer");<br />historyTimer.Start();<br />}<br />//stop timer<br />function StopTimer()<br />{<br />historyTimer.Stop();<br />DisposeTimer();<br />historyTimer = null;<br />}<br />//pause timer<br />function PauseTimer()<br />{<br />//不斷調用由參數一傳遞來的函數checkPause直到函數返回一個true使定時器繼續執行,<br />//調用的時間間隔為第二個參數的值,單位為毫秒,預設值為 500毫秒。<br />//alert("pause");<br />historyTimer.Pause(CheckPause,1000);<br />}<br />//resume timer run<br />function ResumeTimer()<br />{<br />historyTimer.Resume();<br />//alert("resume timer");<br />}<br />function DisposeTimer()<br />{<br />if(historyTimer!=null)<br />{<br />historyTimer.Dispose();<br />}<br />}<br />
註:程式非原創,已忘記出處。