Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const currentTime = document.getElementById('timerCircle');
const currentSessionDuration = document.getElementById('sessionDuration');
const decreaseSessionTimeButton = document.getElementById('decreaseSessionTime');
function appendZeroIfNeeded(){
let currentMinutes = parseInt(currentTime.innerText.split(':')[0]);
if(currentMinutes<11){
currentSessionDuration.innerText = '0'+currentSessionDuration.innerText;
}
}
decreaseSessionTimeButton.addEventListener('click' , ()=>{
if(currentSessionDuration.innerText>0){
currentSessionDuration.innerText = --currentSessionDuration.innerText;
appendZeroIfNeeded();
currentTime.innerText = currentSessionDuration.innerText+':00';
}
});
const increaseSessionTimeButton = document.getElementById('increaseSessionTime');
increaseSessionTimeButton.addEventListener('click' , ()=>{
if(currentSessionDuration.innerText<60){
currentSessionDuration.innerText = ++currentSessionDuration.innerText;
}
});
const currentBreakDuration = document.getElementById('breakDuration');
const decreaseBreakTimeButton = document.getElementById('decreaseBreakTime');
decreaseBreakTimeButton.addEventListener('click' , ()=>{
if(currentBreakDuration.innerText>0){
currentBreakDuration.innerText = --currentBreakDuration.innerText;
}
});
const increaseBreakTimeButton = document.getElementById('increaseBreakTime');
increaseBreakTimeButton.addEventListener('click' , ()=>{
if(currentBreakDuration.innerText<60){
currentBreakDuration.innerText = ++currentBreakDuration.innerText;
}
});
const startTimerButton = document.getElementById('play');
function showCurrentTimeLeft(e){
setInterval(function(){
let currentSeconds = parseInt(currentTime.innerText.split(':')[1]);
let currentMinutes = parseInt(currentTime.innerText.split(':')[0]);
console.log(currentSeconds);
if(currentSeconds==0){
currentMinutes -= 1;
currentTime.innerText = currentMinutes+':'+59;
}
else{
let extraZeroInMinutes = '';
let extraZeroInSeconds = '';
if(currentMinutes<11){
extraZeroInMinutes = '0';
}
if(currentSeconds<11){
extraZeroInSeconds = '0';
}
currentTime.innerText = extraZeroInMinutes+currentMinutes+':'+extraZeroInSeconds+(currentSeconds-1);
}
if(currentSeconds == 0 && currentMinutes == 0){
clearInterval(showCurrentTimeLeft);
}
},1000);
}
startTimerButton.addEventListener('click' , showCurrentTimeLeft);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment