123

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.

            
              <div class="userControls">
  Work Length
  <br>
  <button id="reduceWork">-</button>
  <div id="workLength">7
    <!--should always be set to var workSeconds-->
  </div>
  <button id="increaseWork">+</button>
</div>
<div class="userControls">
  Break Length
  <br>
  <button id="reduceBreak">-</button>
  <div id="breakLength">4
    <!--should always be set to var breakSeconds-->
  </div>
  <button id="increaseBreak">+</button>
</div>


<div id="clockface">
  <div id='fill'></div>
  <div id="time"></div>
  <div id="sessionType">Click to Start or Pause!</div>
</div>

<button id='reset'>Reset</button>
            
          
!
            
              

#clockface {
  position: absolute;
  top: 50px;
  left: 100px;
  height: 300px;
  width: 300px;
  border-style: solid;
  border-width: 0px;
  border-color: red;
  border-radius: 50%;
  overflow: hidden;
  background-color: rgba(255,0,0, 0.5);
  cursor: pointer;
}

#fill {
  position: absolute;
  bottom: 0px;
  left: 0px;
  height: 100%;
  width: 0px;
  background-color: rgba(255,0,0, 0.7);
}

#time {
  position: absolute;
  top: 150px;
  width: 100%;
  text-align: center;
  font-family: mono;
  line-height: 10px;
  color: #fff;
  font-size: 4em;
}

#sessionType {
  position: absolute;
  top: 20%;
  width: 100%;
  text-align: center;
  font-family: mono;
  color: #fff;
  font-size: 1.2em;
}

.start {
  position: absolute;
  top: 10px;
  width: 100%;
  text-align: center;
  font-family: 'sans-serif';
  color: rgba(255,55,255, 1);;
  font-size: 0.8em;
}

#reset {
  position: absolute;
  top: 325px;
  left: 230px;
  height: 50px;
  width: 50px;
  border-style: solid;
  border-radius: 50%;
  border-color: darkred;
  background-color: orange;
  cursor: pointer;
}


.userControls {
  display: flex;
  width: 300px;
  justify-content: center;
}

button:focus {
  outline: 0;
}
            
          
!
            
              // Missing converting seconds to minutes and seconds for display, sounds, session/break length change interface
//

// GLOBAL VARIABLES
// the time in seconds that you want to show with the full clock face
var workSeconds = $("#workLength").text();
var breakSeconds = $("#breakLength").text();
var workTimerSeconds = workSeconds;
var breakTimerSeconds = breakSeconds;

// a counter, decremented at one second intervals by setInterval
var countDown = workTimerSeconds;

// boolean value to determine if the clock is running already
var isRunning = false;

// AG: boolean value to determine session type (work or break)
var onBreak = false;

// used to store the id created by setInterval so it can be stopped with clearInterval later
var intervalID;

// gets the height of the clockface div by looking at the css attribute 'height' you have set your clockface. Shown below in jQuery and pure JS.  You could just hard code heightOfDiv in here and avoid all this.

// get the height of the clockface div using jQuery
var widthOfDiv = parseInt($("#clockface").css("height")); // jQuery

// get the height of the clockface div using pure JS
//var clockDiv = document.getElementById('clockface'); //JS
//var widthOfDiv = parseInt(window.getComputedStyle(clockDiv, null).getPropertyValue("width")); //JS

// INCREASE FILL HEIGHT
// fills the clockface to a height proportional to the time elapsed in seconds
//

function filler(count) {
  //calculate how much of the circle to fill using a simple proportion formula 
  var fillWidth = (workTimerSeconds - count) * widthOfDiv / workTimerSeconds;

  // fill the circle using jQuery
  $("#fill").css("width", fillWidth + "px"); // jQuery

  // fill the circle using JS
  //fill.style.width = fillWidth + 'px'; // JS     
}

function unfiller(count) {
  var fillWidth = (1 - ((breakTimerSeconds - count) / breakTimerSeconds)) * widthOfDiv;

  // fill the circle using jQuery
  $("#fill").css("width", fillWidth + "px"); // jQuery

  // fill the circle using JS
  // fill.style.width = fillWidth + 'px'; // JS   
}

// DISPLAY TIME
// show the current count down value in the clock face
//

function showCount() {
  // show the count down time is seconds on the screen using jQuery    
  $('#time').text(countDown); // jQuery

  // show the count down time in seconds on the screen using pure JS
  // document.getElementById('time').innerHTML = countDown; //JS    
}

// AG: ADJUST SESSION DURATIONS

// reduce length of work session
$("#reduceWork").on("click", function() {
  workSeconds--;
  workTimerSeconds = workSeconds;
  countDown = workTimerSeconds;
  var workLength = $("#workLength").text(); 
  $("#workLength").text(workTimerSeconds);
});

// increase length of work session
$("#increaseWork").on("click", function() {
  workSeconds++;
  workTimerSeconds = workSeconds;
  countDown = workTimerSeconds;
  var workLength = $("#workLength").text(); 
  $("#workLength").text(workTimerSeconds);
});

// reduce length of break session


// increase length of break session


// STOP / PAUSE
// waits for the user to click the clock face and either starts or pauses 
// the timer based on the state of the boolean isRunning
//

$('#clockface').on("click", function() { //jQuery
  // document.getElementById("clockface").onclick = function() { // JS

  // see if the timer is already running
  if (!isRunning) {
    // start a timer that calls the function decTime every second. It's id is stored in intervalID so we can  stop it later with clearInterval(intervalID) 
    intervalID = setInterval(decTime, 1000);
    // set boolean to show timer is running
    isRunning = true;
  } else {
    // stop the timer
    clearInterval(intervalID);
    // set boolean to timer isn't running
    isRunning = false;
  }
});

// RESET
// waits for the user to click the reset button and stops the clock 
// if it's running and resets the values to start again
//

$('#reset').click(function(){  //jQuery
// document.getElementById("reset").onclick = function() { // JS

  // see if the timer is already running
  if (isRunning) {
    // stop the timer
    clearInterval(intervalID);
    // set boolean to show timer is not running
    isRunning = false;
  }
  $("#sessionType").text("Click to Start or Pause!");
  // initalize countDown to session length in seconds
  countDown = workTimerSeconds;
  // reset boolean to show not on break
  onBreak = false;
  // clear and show timer
  filler(countDown);
  // display the current count down
  showCount();
});

// MAIN CONTROL LOOP
//
// Move the filler across and back based on session type
// Show the current count down and decrement the count down. 
// Swap session types
//

function decTime() {
  // AG: check to see if timer is for work or break
  if (!onBreak) {
    $("#sessionType").text("Time to work, stay focused!");
    // fill the circle
    filler(countDown);
    // display the current count down
    showCount();
    // decrement the counter if it's not at zero
    if (countDown > 0) {
      countDown--;
    } else {
      // AG: transition from session to break
      onBreak = true;
      countDown = breakTimerSeconds;
    }
  } else {
    $("#sessionType").text("Relax, you earned it!");
    // unfill the circle
    unfiller(countDown);
    // display the current count down
    showCount();
    // decrement the counter if it's not at zero
    if (countDown > 0) {
      countDown--;
    } else {
      // AG: transition from break to session
      onBreak = false;
      countDown = workTimerSeconds;
    }
  }
};

//
// initial setup on first run
//

// initial clearing of clockface
filler(countDown);
// display the current count down in seconds
showCount();
            
          
!
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.

Console