Skip to content

Instantly share code, notes, and snippets.

@Zabanaa
Created August 17, 2015 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zabanaa/2eff2f292786b8d64418 to your computer and use it in GitHub Desktop.
Save Zabanaa/2eff2f292786b8d64418 to your computer and use it in GitHub Desktop.
New pomodoro hahah
<script>
// Create a counter to check how many times the function was called
var numberSessionsDone = 0;
var initialTime = 25;
var output = document.getElementById('output');
// this is the countDown timer
function runSession(timeInMinutes, endFunction) {
var timeLeft = timeInMinutes;
render(timeLeft); // Render the initial value
var countDown = setInterval(function() {
timeLeft--;
render(timeLeft);
// when the timer runs out
if ( timeLeft === 0 ) {
// Clear the interval
clearInterval(countDown);
// Run the end function
endFunction();
}
}, 1000);
}
// Runs the 25 minutes timer
function startSession() {
console.log( "starting work session number " + (numberSessionsDone + 1) );
runSession(25, finishSession);
}
function finishSession(){
// increase the counter
numberSessionsDone++;
// console.log(numberSessionsDone);
if ( numberSessionsDone <= 3 ) {
// if the sessions done are less than 3, start a short break
console.log( "starting short break number " + numberSessionsDone );
startShortBreak();
} else if ( numberSessionsDone == 4 ) {
// else if the sessions done are equal to 4 then start a long break
console.log("starting long break");
startLongBreak();
numberSessionsDone = 0; // And reset the counter back to 0
}
}
// This runs a 5 minutes timer for the short break
function startShortBreak() {
runSession(5, finishShortBreak);
}
// When the break is finished run another 25 minutes session
function finishShortBreak() {
// console.log("new work session");
startSession();
}
// when the fourth session is finished, start a 15 minutes long break
function startLongBreak() {
// console.log("start long break");
runSession(15, finishLongBreak);
}
// when the long break is finished reset the timer
function finishLongBreak() {
render(initialTime);
}
// render function
function render(timeToRender) {
output.innerHTML = timeToRender;
}
document.body.addEventListener("click", startSession, false);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment