aboutsummaryrefslogtreecommitdiff
path: root/pointless/viewer/timer.js
blob: 28c3f97bfb8498bc48c65b24754d38995426f73a (plain)
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
/*
 * timer.js
 * Part of Pointless Viewer, a Beamer presentation viewer
 * Copyright 2018 Pacien TRAN-GIRARD
 * License: GNU GPL v3
 */

"use strict";

class Timer {
  constructor(window) {
    this.display = window.document.getElementById("timer");
    this.startTime = null;
    this._setDisplay(0);
  }

  start() {
    if (this.startTime != null) return;
    this.startTime = Date.now();

    const self = this;
    setInterval(function() {
      self._runTimer();
    }, 1000);
  }

  _runTimer() {
    const timeDelta = Math.floor((Date.now() - this.startTime) / 1000);
    this._setDisplay(timeDelta);
  }

  _setDisplay(seconds) {
    const dateObj = new Date(null);
    dateObj.setSeconds(seconds);
    this.display.textContent = dateObj.toISOString().substr(11, 8);
  }
}