All files / src timer.ts

86.67% Statements 13/15
100% Branches 4/4
71.43% Functions 5/7
85.71% Lines 12/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 511x                   35x 35x 35x 35x                                 2x 2x           2x 2x         1x       2x 2x    
export class Timer {
 
    private elapsed: number;
    private timer: any;
    private interval: number;
    private callback: any;
    private active: boolean;
 
    constructor(interval: number = 100, callback: any = null) {
 
        this.interval = interval;
        this.elapsed = 0;
        this.callback = callback;
        this.active = false;
    }
 
    private get window(): Window {
 
        return window;
    }
 
    public get Elapsed(): number {
 
        return this.elapsed;
    }
 
    public start(): void {
 
        if (!this.active) {
 
            this.timer = this.window.setInterval(this.increment.bind(this), this.interval);
            this.active = true;
        }
    }
 
    public pause(): void {
 
        this.window.clearInterval(this.timer);
        this.active = false;
    }
 
    public reset(): void {
 
        this.elapsed = 0;
    }
 
    private increment(): void {
        this.elapsed += this.interval;
        if (this.callback) this.callback();
    }
}