Skip to content

Instantly share code, notes, and snippets.

@jayjariwala
Last active January 11, 2018 18:56
Show Gist options
  • Save jayjariwala/d97d98e8541becf597fd76591cb90d82 to your computer and use it in GitHub Desktop.
Save jayjariwala/d97d98e8541becf597fd76591cb90d82 to your computer and use it in GitHub Desktop.
Pomodoro Clock
// desgin inspiration https://dribbble.com/shots/2324994-Daily-UI-Day-14-Countdown-Timer/attachments/442297
(function(){
let focusSecs;
let breakSecs;
let timer;
let isPaused = false;
let pausedSecs = 0;
function displayTimer(secs)
{
let hr = Math.floor((secs / (60 * 60)) % 24);
let mins = Math.floor(secs / 60) % 60;
let seconds = secs % 60;
let displayTime = `${hr}:${mins < 10 ? '0'+ mins : mins}:${seconds < 10 ? '0'+ seconds : seconds}`;
console.log(displayTime);
}
function pomodoroTimer(secs)
{
if(pausedSecs === 0)
{
displayTimer(secs);
let then = Date.now() + (secs * 1000);
runTimer(then);
}
else
{
let then = Date.now() + (pausedSecs * 1000);
pausedSecs = 0;
runTimer(then);
}
function runTimer(then)
{
timer = setInterval(() => {
let timeLeft = Math.round((then - Date.now()) / 1000);
if(isPaused)
{
console.log("== Paused ==");
pausedSecs = timeLeft + 1;
clearInterval(timer);
return;
}
if (timeLeft < 0) {
clearInterval(timer);
[focusSecs,breakSecs] = [breakSecs,focusSecs];
pomodoroTimer(focusSecs);
return;
}
displayTimer(timeLeft);
} , 1000);
}
}
function start(focusMins, breakMins)
{
focusSecs = focusMins * 60;
breakSecs = breakMins * 60;
pomodoroTimer(focusSecs);
}
function pause()
{
isPaused = true;
}
function resume()
{
isPaused = false;
pomodoroTimer(focusSecs);
}
function reset()
{
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment