css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h1>Pomodoro Timer</h1>
<header>
	<div class="breakCtrl">
		<p>Break Length </p>
		<button class="button-reset minus" onClick='decreaseBreak()'>-</button>
		<span id="break">5</span>
		<button class="button-reset" onClick='increaseBreak()'>+</button>
	</div>
	<div class="sessionCtrl">
		<p>Session Length </p>
		<button class="button-reset minus" onClick='decreaseSession()'>-</button>
		<span id="session">25</span>
		<button onClick='increaseSession()'>+</button>
	</div>
</header>

<button id='reset' class="reset" onclick='resetMachine()'></button>

<div onClick='startTheMachine()' title="Click to start/pause" class='machine'>


	<div class="timer">
		<svg class="self-center" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 300 300" preserveAspectRatio="none">
    <circle cx="40%" cy="50%" r="57" id="halo" fill="none" stroke="#00CC33" stroke-width="5" stroke-dasharray="360,20000" transform="rotate(-90,100,100)" />
    <text id="session-label"  x="41%" y="24%" fill="#868e96" font-family: "Open Sans">Session</text>
    <text id="myTimer"  x="43%" y="34%" fill="#868e96" font-family: "Open Sans"></text>
  </svg>
	</div>

</div>

            
          
!
            
              $back: $oc-gray-0;
$fore: $oc-gray-6;
@import url('https://fonts.googleapis.com/css?family=Open+Sans:300');

body{
	height: 100%;
	overflow: hidden;
	background-color: $back;
	position: relative;
	font-family: 'Open Sans';
	margin-top: 9em;
}


h1 {
	display: block;
	background-color: $back;
	margin: 0 auto;
	color: $fore;
	text-align: center;
	font-family: 'Open Sans';
	font-size: 5em;
}

button {
	background-color: $back;
	color: $fore;
	border: none;
	cursor: pointer;
	font-size: 2em;
	outline: none;
}
.reset{
	display: block;
	margin: 0 auto;
}
#session, #break{
	font-size: 2em;
}
header {
	text-align: center;
	margin: 0 auto;
	color: $fore;
	text-transform: uppercase;
	padding: 20px;
	font-size: 1em;
}
.machine {
	max-height: 13em;
}
.breakCtrl,
.sessionCtrl {
	display: inline-block;
	padding-left: 30px;
	padding-right: 30px;
}

.self-center {
	width: 30em;
  height: 30em;
  color: $fore;
  cursor: pointer;
  overflow: hidden;
	text-align: center;
	margin-bottom: -10em;
}

.timer{
	width: 30em;
	margin: 0 auto;
	text-align: center;
}

            
          
!
            
              let seconds = 25 * 60;
let breakSeconds = 5 * 60;
let isBreak = false;
const circle = document.getElementById('halo');
const myTimer = document.getElementById('myTimer');
const headline = document.getElementById('session-label');
let lockout = false;
let t = new Timer();
let currentlyRunning = false;
let isPaused = false;


function Timer() {
	var startTime, endTime, instance = this;
	//pass in the total number of seconds to count down
	this.start = function(seconds) {
		startTime = new Date();
		endTime = new Date();
		endTime.setSeconds(startTime.getSeconds() + seconds);

	};

	//reset timer
	this.clear = function() {
		startTime = null;
		endTime = null;
	}
	//format days:hours:minutes:seconds
	//will always print minutes:seconds
	this.print = function() {
		var out = "";
		if (instance.getDays() >= 1) {
			out += Math.floor(instance.getDays()) + ":"
		}
		if (instance.getHours() >= 1) {
			out += Math.floor(instance.getHours()) + ":"
		}
		if (instance.getSeconds() > 0) {
			out += Math.floor(instance.getMinutes()) + ":" + ("0" + instance.getSeconds() % 60).slice(-2);
		}
		return out;
	}

	this.getMilliseconds = function() {
		startTime = new Date();
		if (!endTime) {
			return 0;
		}
		return endTime.getTime() - startTime.getTime();
	}

	this.getSeconds = function() {
		startTime = new Date();
		if (!endTime) {
			return 0;
		}
		//console.log(Math.round((endTime.getTime() - startTime.getTime()) / 1000));
		return Math.round((endTime.getTime() - startTime.getTime()) / 1000);
	}

	this.getMinutes = function() {
		return instance.getSeconds() / 60;
	}
	this.getHours = function() {
		return instance.getMinutes() / 60 / 60;
	}
	this.getDays = function() {
		return instance.getHours() / 24;
	}
}

function drawCircle(seconds) {
	var circle = document.getElementById('halo');
	var timeNow = new Date();
	var interval = seconds;
	var angle = 360;
	var angle_increment = .36;
	var count = seconds - 1;
	var t = new Timer();
	var isBreak = false;
	//sets the timer for the duration
	t.start(seconds);
	//setting the label in the circle
	myTimer.innerHTML = t.print();
	console.log('draw circle')
	//keeping the label up to date as time goes on
	window.counter = window.setInterval(function() {
		myTimer.innerHTML = t.print();
	}, 1000);
	//every 100 ms for a smooth ticking away of the circle
	window.timer = window.setInterval(function() {
		console.log('update circle')
		console.log(t.print())
		circle.setAttribute("stroke-dasharray", angle + ", 20000");
		if (angle <= 0 || t.getMilliseconds() < 0) {
			t.clear();
			//time has run out, stop the label updating and the angle change
			window.clearInterval(window.timer);
			window.clearInterval(window.counter);
			return;
		}
		angle = 360 * t.getMilliseconds() / (seconds * 1000);
	}, 100);
}

startTheMachine = function() {
	document.getElementById('reset').innerHTML = 'reset';
	if (!currentlyRunning) {
		currentlyRunning = true;
		headline.innerHTML = 'Session';
		t.start(seconds);
		window.machine = window.setInterval(function() {
			if (!lockout) {
				lockout = true;
				if (isBreak) {
					t.start(breakSeconds);
					circle.style.stroke = 'red';
					drawCircle(breakSeconds);
					window.timeout1 = window.setTimeout(function() {
						lockout = false;
						t.clear();
						isBreak = !isBreak;
						headline.innerHTML = 'Session';
					}, breakSeconds * 1000);
				} else {
					t.start(seconds);
					circle.style.stroke = '#00CC33';
					drawCircle(seconds);
					window.timeout1 = window.setTimeout(function() {
						lockout = false;
						isBreak = !isBreak;
						t.clear();
						headline.innerHTML = 'Break';
					}, seconds * 1000);
				}
			}
		}, 50);
	} else {
		if (!isPaused) {
			isPaused = !isPaused;
			secondsRemaining = t.getSeconds();
			t.clear();
			isPaused = true;
			window.clearInterval(window.counter);
			window.clearInterval(window.timer);
			window.clearInterval(window.machine);
			window.clearTimeout(window.timeout1);
		} else {
			isPaused = !isPaused;
			t.start(secondsRemaining);
			drawCircle(secondsRemaining);
			window.timeout1 = window.setTimeout(function() {
				lockout = false;
				isBreak = !isBreak;
				t.clear();
				currentlyRunning = false;
				startTheMachine();
			}, secondsRemaining * 1000);
		}
	}
}

increaseSession = function() {
	seconds += 60;
	document.getElementById("session").innerHTML = seconds / 60;
	if(!currentlyRunning){
		myTimer.innerHTML = seconds / 60 + ":" + ("0" + seconds % 60).slice(-2);
	}
}
decreaseSession = function() {
	if (seconds > 0) {
		seconds -= 60;
		document.getElementById("session").innerHTML = seconds / 60;
	if(!currentlyRunning) {
	myTimer.innerHTML = seconds / 60 + ":" + ("0" + seconds % 60).slice(-2);
}

	}

}
increaseBreak = function() {

	breakSeconds += 60;
	document.getElementById("break").innerHTML = breakSeconds / 60;
}
decreaseBreak = function() {
	if (breakSeconds > 0) {
		breakSeconds -= 60;
		document.getElementById("break").innerHTML = breakSeconds / 60;
	}
}

resetMachine = function() {
	window.clearInterval(window.counter);
	window.clearInterval(window.timer);
	window.clearInterval(window.machine);
	window.clearTimeout(window.timeout1);
	t.clear();
	reset = false;
	currentlyRunning = false;
	lockout = false;
	myTimer.innerHTML = Math.floor(seconds / 60) + ":" + ("0" + seconds % 60).slice(-2);
	isBreak = false;
	headline.innerHTML = 'Session';
	document.getElementById('reset').innerHTML = 'reset';

};
myTimer.innerHTML = Math.floor(seconds / 60) + ":" + ("0" + seconds % 60).slice(-2);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console