Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update timer.js #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 14 additions & 50 deletions src/js/timer.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,25 @@
import { table } from "./app.js";

var interval;
const secondsElement = document.getElementById('seconds');
const minutesElement = document.getElementById('minutes');
const hoursElement = document.getElementById('hours');

export function startTimer(){
let timeCounter = 1;
let seconds, minutes, hours = 0;
let interval = 0;
let timeCounter = 0;

let secondsElement = document.getElementById('seconds');
let minutesElement = document.getElementById('minutes');
let hoursElement = document.getElementById('hours');
function setTime(seconds) {
secondsElement.textContent = `0${seconds % 60}`.slice(-2);
minutesElement.textContent = `0${Math.floor(seconds / 60) % 60}`.slice(-2);
hoursElement.textContent = `0${Math.floor(seconds / 60 / 60)}`.slice(-2);
}

export function startTimer() {
resetTimeCounter();
clearInterval(interval);

table.timeCounterIsStart = true;

interval = setInterval(() => {
if (timeCounter < 60){
seconds = timeCounter;
if (seconds < 10){
seconds = "0" + seconds;
}
secondsElement.innerHTML = seconds;
} else if (timeCounter >=60 && timeCounter < 3600){
seconds = timeCounter % 60;
if (seconds < 10){
seconds = "0" + seconds;
}
minutes = Math.floor(timeCounter / 60);
if (minutes < 10) {
minutes = "0" + minutes;
}
secondsElement.innerHTML = seconds;
minutesElement.innerHTML = minutes;
} else if (timeCounter >= 3600){
seconds = timeCounter % 60;
if (seconds < 10){
seconds = "0" + seconds;
}
minutes = Math.floor(timeCounter / 60 % 60);
if (minutes < 10) {
minutes = "0" + minutes;
}
hours = Math.floor(timeCounter / 3600);
if (hours < 10) {
hours = "0" + hours;
}
secondsElement.innerHTML = seconds;
minutesElement.innerHTML = minutes;
hoursElement.innerHTML = hours;
}
timeCounter++
}, 1000);
interval = setInterval(() => setTime(++timeCounter), 1000);
}

export function stopTimer() {
Expand All @@ -59,10 +28,5 @@ export function stopTimer() {
}

export function resetTimeCounter() {
let secondsElement = document.getElementById('seconds');
let minutesElement = document.getElementById('minutes');
let hoursElement = document.getElementById('hours');
secondsElement.innerHTML = "00";
minutesElement.innerHTML = "00";
hoursElement.innerHTML = "00";
setTime(timeCounter = 0);
}