All files / src time.js

0% Statements 0/55
0% Branches 0/1
0% Functions 0/1
0% Lines 0/55

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 51 52 53 54 55 56                                                                                                               
export const Time = {

  // formats time from seconds into hours, minutes and seconds
  formatTime(time) {
    const hrs = Math.floor(time / 60 / 60);
    const mins = Math.floor((time / 60) % 60);
    const secs = Math.floor(time % 60);

    let ret = '';
    if (hrs > 0) {
      ret += `${hrs}:${mins < 10 ? '0' : ''}`;
    }
    ret += `${mins}:${secs < 10 ? '0' : ''}`;
    ret += `${secs}`;
    return ret;
  },

  // starts counting up from the set parameter
  ingameTime(seconds) {
    let time = seconds;
    document.getElementById('ingameTime').innerHTML = this.formatTime(time);
    time++;
    setInterval(() => {
      document.getElementById('ingameTime').innerHTML = this.formatTime(time);
      time++;
    }, 1000);
  },

  // creates a timer that counts from parameter to zero
  setTimer(seconds) {
    // Checks whether parameter is positive, if not, clears the timer.
    if (seconds <= 0) {
      document.getElementById('timer').style.visibility = 'hidden';
      document.getElementById('timer').innerHTML = '';
      return;
    }
    let time = seconds;
    document.getElementById('timer').innerHTML = this.formatTime(time);
    document.getElementById('timer').style.visibility = 'visible';
    time--;

    const timer = setInterval(() => {
      // if timer runs out, this function returns a 1
      if (time <= 0) {
        clearInterval(timer);
        document.getElementById('timer').style.visibility = 'hidden';
        document.getElementById('timer').innerHTML = '';
      } else { // if timer didnt run out, decrease time by 1 second
        document.getElementById('timer').innerHTML = this.formatTime(time);
        time--;
      }
      // the interval in which the time gets updated (1000 = 1 second)
    }, 1000);
  },
};