创建多个定时器(setInterval),包含了开始、停止方法。
var timer = {
setTimeout: {
start: function(_timerName, _func, _interval) {
if (timer.setTimeout[_timerName]) {
timer.setTimeout.stop(_timerName);
}
timer.setTimeout[_timerName] = setTimeout(_func, _interval || 1000);
},
stop: function(_timerName) {
clearTimeout(timer.setTimeout[_timerName]);
timer.setTimeout[_timerName] = null;
}
},
setInterval: {
start: function(_timerName, _func, _interval) {
if (timer.setInterval[_timerName]) {
timer.setInterval.stop(_timerName);
}
timer.setInterval[_timerName] = setInterval(_func, _interval || 1000);
},
stop: function(_timerName) {
clearInterval(timer.setInterval[_timerName]);
timer.setInterval[_timerName] = null;
}
}
};
使用
function recordStatus1() {
console.log('recordStatus1');
}
function recordStatus2() {
console.log('recordStatus2');
}
------- 开始 ------
timer.setTimeout.start('RecordTimer1', recordStatus1,1000);
timer.setInterval.start('RecordTimer2', recordStatus2,1000);
------- 停止 ------
timer.setTimeout.stop('RecordTimer1');
timer.setInterval.stop('RecordTimer2');
评论 (0)