学习js,因为函数和定时器直接的关系是非绑定的。我不能说我开启一个定时,然后拿一个变量去记录这个setInterval的返回值,这样很麻烦。
本着面向对象的思想,利用业余时间(周末在家里)写了一个类,用来管理定时器。
吐槽下jQuery,不知道是不是我的jQuery版本太低,貌似jQuery没有提供很好的定时器管理类。
好废话不多说,上代码:
function TimeControlEx() { var nCount = 0; var oTimerIDs = []; //ID,Name var that = this; var nTimerId = 0; this.ExecOnce = function (funcid) { if (oTimerIDs[funcid].Limit == 0) { //只要归零了,肯定是定时的,对于这种,就得关闭 clearInterval(oTimerIDs[funcid].TimerID); } }; this.add = function (nSec, oFunction, sName, nTimes) { ///<param name="nSec">延迟时间,毫秒</param> ///<param name="oFunction">调用函数</param> ///<param name="sName">定时器名称(可选)</param> ///<param name="nTimes">循环次数(可选)</param> //todo:根据各个参数来创建,基础的参数是nSec和oFunction var nParams = arguments.length; var obj; switch (nParams) { case 2: //两个参数,自动增加 nTimerId = setInterval(oFunction, nSec); obj = { ID: nCount, TimerID:nTimerId, Name: String(nCount), Interval: nSec, Limit: -1 }; oTimerIDs.push(obj); break; case 3: //三个参数 nTimerId = setInterval(oFunction, nSec); obj = { ID: nCount, TimerID: nTimerId, Name: sName, Interval: nSec, Limit: -1 }; oTimerIDs.push(obj); break; case 4: //四个参数 obj = { ID: nCount, TimerID: nTimerId, Name: sName, Interval: nSec, Limit: nTimes }; oTimerIDs.push(obj); break; default: return; } }; this.CloseTimer = function (sTimerName) { ///<param name="sTimerName">计时器的名字,或ID</param> if (typeof (sTimerName) == "number") { //按照数字方法关闭 for (var j = 0; j < oTimerIDs.length; j++) { if (oTimerIDs[j].ID == sTimerName) { clearInterval(oTimerIDs[j].TimerID); oTimerIDs = oTimerIDs.splice(j, 1); nCount--; break; } } } else { //按照字符串方式关闭 for (var m = 0; m < oTimerIDs.length; m++) { if (oTimerIDs[m].Name == sTimerName) { clearInterval(oTimerIDs[m].TimerID); oTimerIDs.splice(m, 1); nCount--; break; } } } }; }
编辑:今天查看后,发现代码存在严重的bug,修复了一下,现在放出修改后的代码:
1 function TimeControlEx() { 2 var nCount = 0; 3 var oTimerIDs = []; //ID,Name 4 var that = this; 5 var nTimerId = 0; 6 this.ExecOnce = function (funcid) { 7 if (oTimerIDs[funcid].Limit == 0) { 8 //只要归零了,肯定是定时的,对于这种,就得关闭 9 clearInterval(oTimerIDs[funcid].TimerID); 10 } 11 }; 12 this.add = function (nSec, oFunction, sName, nTimes) { 13 ///<param name="nSec">延迟时间,毫秒</param> 14 ///<param name="oFunction">调用函数</param> 15 ///<param name="sName">定时器名称(可选)</param> 16 ///<param name="nTimes">循环次数(可选)</param> 17 //todo:根据各个参数来创建,基础的参数是nSec和oFunction 18 var nParams = arguments.length; 19 var obj; 20 switch (nParams) { 21 case 2: 22 //两个参数,自动增加 23 nTimerId = setInterval(oFunction, nSec); 24 obj = { 25 ID: nCount, 26 TimerID: nTimerId, 27 Name: String(nCount), 28 Interval: nSec, 29 Limit: -1 30 }; 31 oTimerIDs.push(obj); 32 break; 33 case 3: 34 //三个参数 35 nTimerId = setInterval(oFunction, nSec); 36 obj = { 37 ID: nCount, 38 TimerID: nTimerId, 39 Name: sName, 40 Interval: nSec, 41 Limit: -1 42 }; 43 oTimerIDs.push(obj); 44 break; 45 case 4: 46 //四个参数 47 obj = { 48 ID: nCount, 49 TimerID: 0, 50 Name: sName, 51 Interval: nSec, 52 Limit: nTimes 53 }; 54 var oFunc = function (oThis, oMyFunc) { 55 var oThat = oThis; 56 var oMyFun = oMyFunc; 57 var oRun = function () { 58 if (oThat.Limit > 0 || oThat.Limit == -1) { 59 oMyFun(); //执行传入的函数 60 if (oThat.Limit > 0) oThat.Limit--; 61 } else { 62 //自行了断 63 clearInterval(oThat.TimerID); 64 } 65 }; 66 return oRun; 67 }; 68 nTimerId = setInterval(oFunc(obj, oFunction), nSec); 69 obj.TimerID = nTimerId; 70 oTimerIDs.push(obj); 71 break; 72 default: 73 return; 74 } 75 }; 76 this.CloseTimer = function (sTimerName) { 77 ///<param name="sTimerName">计时器的名字,或ID</param> 78 if (typeof (sTimerName) == "number") { 79 //按照数字方法关闭 80 for (var j = 0; j < oTimerIDs.length; j++) { 81 if (oTimerIDs[j].ID == sTimerName) { 82 clearInterval(oTimerIDs[j].TimerID); 83 oTimerIDs = oTimerIDs.splice(j, 1); 84 nCount--; 85 break; 86 } 87 } 88 } else { 89 //按照字符串方式关闭 90 for (var m = 0; m < oTimerIDs.length; m++) { 91 if (oTimerIDs[m].Name == sTimerName) { 92 clearInterval(oTimerIDs[m].TimerID); 93 oTimerIDs.splice(m, 1); 94 nCount--; 95 break; 96 } 97 } 98 } 99 }; 100 }
谢谢大家的支持