TimerCounter.js 1.5 KB
// //////////////////////////////////////////////////////////////////////////////
//  计时器
// //////////////////////////////////////////////////////////////////////////////

//import ApeConsts from './ApeConsts';
//import Loger from 'Loger';
//import MessageTypes from 'MessageTypes';
//import GlobalConfig from 'GlobalConfig';
//import EngineUtils from 'EngineUtils';

//let loger = Loger.getLoger('MediaModule');

let counter=0;
let callBackDelay=1;
let callBackFun;
let isStart=false;
class TimerCounter {
    constructor() {
        this.timer=0;
        this.delay=1000;
    }

    addTimerCallBack(_callBackFun,_callBackDelay){
        callBackFun=_callBackFun;
        callBackDelay=_callBackDelay;
    }
    //开计时
    startTimer(_position=0) {
        if(isStart) return;
        isStart=true;
        if(_position&&parseInt(_position)>0){
            counter=_position;
        }else {
            counter=0;
        }
        console.log("startTimer",counter);
        this.timerClear();
        this.timerStart();

    }
    //停止
    stopTimer(){
        console.log("stopTimer",counter);
        isStart=false;
        this.timerClear();
    }
    //计数
    updateCounter(){
        counter++;
        //this.counter++;
        //console.log("TimerCounter",counter);
        if(callBackFun!=null&&counter%callBackDelay==0){
            callBackFun();
        }
    }
    timerStart(){
        this.timer= setInterval(this.updateCounter, this.delay);
    }
    timerClear(){
        clearInterval(this.timer);
    }
}
export default TimerCounter;