// //////////////////////////////////////////////////////////////////////////////
// 计时器
// //////////////////////////////////////////////////////////////////////////////
class TimerCounter {
constructor() {
this.timer=0;
this.delay=1000;
this.counter=0;
this.callBackDelay=1;//回调间隔,单位(秒)
this.callBackFun=null;//回调函数
this.isStart=false;
}
addTimerCallBack(_callBackFun,_callBackDelay){
this.callBackFun=_callBackFun;
this.callBackDelay=_callBackDelay;
}
//开计时
startTimer(_position=0) {
if(this.isStart) return;
this.isStart=true;
if(_position&&parseInt(_position)>0){
this.counter=_position;
}else {
this.counter=0;
}
console.log("startTimer",this.counter);
this.timerClear();
this.timerStart();
}
//停止
stopTimer(){
if(!this.isStart) return;
console.log("stopTimer",this.counter);
this.isStart=false;
this.timerClear();
}
//计数
updateCounter(){
this.counter++;
//this.counter++;
//console.log("TimerCounter",counter);
if(this.callBackFun!=null&&this.counter%this.callBackDelay==0){
this.callBackFun();
}
}
timerStart(){
this.timer= setInterval(this.updateCounter.bind(this), this.delay);
}
timerClear(){
clearInterval(this.timer);
}
}
export default TimerCounter;