解决了以下问题:
1 实现传参数和改变作用域
2 保证定时触发器,触发间隔。
调用方式:
1 method:执行的方法 使用的参数为paramters [参数1,参数2]
paramters:传递的参数,形式 [参数1,参数2]
milliseconds:执行间隔 1000代表1秒
scope:代表作用域
GLOBAL.Timer.setInterval(method, paramters, milliseconds,scope);
GLOBAL.Timer.setTimeout(method, parameters, milliseconds,scope);
1 GLOBAL.namespace("Timer"); 2 GLOBAL.Timer._callTimeOutMethod = function(method, parameters,scope) { 3 return function() { 4 if (typeof (parameters) == "undefined" ||typeof (parameters) == "null") { 5 if(typeof(scope) == "undefined"||typeof(scope) == "null") 6 { 7 method(); 8 }else 9 { 10 method.apply(scope); 11 } 12 } else { 13 if(typeof(scope) == "undefined"||typeof(scope) == "null") 14 { 15 method(parameters); 16 }else 17 { 18 method.apply(scope,parameters); 19 } 20 } 21 } 22 }; 23 GLOBAL.Timer._callIntervalMethod = function(method, parameters, milliseconds,scope) { 24 return function() { 25 if (typeof (parameters) == "null") { 26 if(typeof(scope) == "undefined"||typeof(scope) == "null") 27 { 28 method();} 29 else 30 { 31 method.apply(scope); 32 } 33 } else { 34 if(typeof(scope) == "undefined"||typeof(scope) == "null") 35 { 36 method(parameters);} 37 else 38 { 39 method.apply(scope,parameters); 40 } 41 } 42 window.setTimeout(arguments.callee, milliseconds); 43 } 44 }; 45 /** 46 * 调用函数 参数method 需要以数组作为参数 47 */ 48 GLOBAL.Timer.setTimeout = function(method, parameters, milliseconds,scope) { 49 if (typeof (parameters) == "null") { 50 if(typeof(scope) == "undefined") 51 { 52 return window.setTimeout(GLOBAL.Timer._callTimeOutMethod(method), 53 milliseconds); 54 }else 55 { 56 return window.setTimeout(GLOBAL.Timer._callTimeOutMethod(method,null,scope), 57 milliseconds); 58 } 59 } else { 60 if(typeof(scope) == "undefined") 61 { 62 return window.setTimeout(GLOBAL.Timer._callTimeOutMethod(method, 63 parameters), milliseconds); 64 }else 65 { 66 return window.setTimeout(GLOBAL.Timer._callTimeOutMethod(method, 67 parameters,scope), milliseconds); 68 } 69 } 70 }; 71 72 GLOBAL.Timer.clearTimeout = function(timerID) { 73 return window.clearTimeout(timerID); 74 }; 75 GLOBAL.Timer.clearInterval = function(timerIntervalID) { 76 return GLOBAL.Timer.clearTimeout(timerIntervalID); 77 }; 78 /** 79 * 安全的间隔执行 以setTimeout实现 80 */ 81 GLOBAL.Timer.setInterval = function(method, paramters, milliseconds,scope) { 82 if (typeof (parameters) == "null") { 83 if(typeof (scope) == "undefined") 84 { 85 return window.setTimeout(GLOBAL.Timer._callIntervalMethod(method, null, 86 milliseconds), milliseconds); 87 }else 88 { 89 return window.setTimeout(GLOBAL.Timer._callIntervalMethod(method, null, 90 milliseconds,scope), milliseconds); 91 } 92 } else { 93 if(typeof (scope) == "undefined") 94 { 95 return window.setTimeout(GLOBAL.Timer._callIntervalMethod(method, 96 parameters, milliseconds), milliseconds); 97 }else 98 { 99 return window.setTimeout(GLOBAL.Timer._callIntervalMethod(method, null, 100 milliseconds,scope), milliseconds); 101 } 102 } 103 };
时间: 2024-12-18 03:42:57