You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.3 KiB

let startTime = 0;
let savedPreviousSeconds = 0;
let running = false;
let windowInterval;
const previousSessionAccumulatedSeconds = parseInt(window.localStorage.getItem('savedPreviousSeconds'));
if(previousSessionAccumulatedSeconds){
savedPreviousSeconds = previousSessionAccumulatedSeconds
}
const padDigits = (value) => {
if(value < 10){
return '0'+value;
} else {
return value;
}
}
const formatSeconds = (total) => {
const hours = Math.floor(total/60/60);
const minutes = Math.floor( (total - (hours*60*60)) / 60 );
const seconds = total - minutes*60 - hours * 60 * 60;
return `${padDigits(hours)}:${padDigits(minutes)}:${padDigits(seconds)}`;
}
const getAccumulatedSeconds = (newerTime, olderTime) => {
return Math.floor((newerTime-olderTime)/1000);
}
const displayTime = () => {
const totalSeconds = getAccumulatedSeconds(Date.now(),startTime) + savedPreviousSeconds;
document.querySelector('h1').innerHTML = formatSeconds(totalSeconds);
}
const updateSavedPreviousSeconds = () => {
savedPreviousSeconds += getAccumulatedSeconds(Date.now(), startTime);
}
document.querySelector('h1').innerHTML = formatSeconds(savedPreviousSeconds);
document.querySelector('.btn-primary').addEventListener('click', (event) => {
startTime = Date.now();
running = true;
document.querySelector('.btn-primary').disabled=true;
document.querySelector('.btn-secondary').disabled=false;
document.querySelector('.btn-danger').disabled=true;
windowInterval = window.setInterval(displayTime,1000)
})
document.querySelector('.btn-secondary').addEventListener('click', (event) => {
updateSavedPreviousSeconds();
running = false;
document.querySelector('.btn-primary').disabled=false;
document.querySelector('.btn-secondary').disabled=true;
document.querySelector('.btn-danger').disabled=false;
window.clearInterval(windowInterval)
})
document.querySelector('.btn-danger').addEventListener('click', (event) => {
savedPreviousSeconds = 0;
document.querySelector('h1').innerHTML = formatSeconds(0);
document.querySelector('.btn-danger').disabled=true;
})
window.onbeforeunload = function(){
if(running){
updateSavedPreviousSeconds();
}
window.localStorage.setItem('savedPreviousSeconds', savedPreviousSeconds);
return 'Good bye';
}