Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <!--  Bootstrap CSS  -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  <!--  Google Fonts  -->
  <link href="https://fonts.googleapis.com/css?family=Comfortaa" rel="stylesheet">
  <!--  Bootstrap JavaScript  -->
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
  <title>Pomodoro Clock</title>
</head>

<body id="body">
  <div class="container">
    <h1>Pomodoro Clock</h1>
    <div id="pomodoro-clock"></div>
    <footer class="footer">
      <small>Built by <a href="https://www.charonworks.com" target="_blank">Charonworks</a></small>
    </footer>
  </div>

  <audio id="beep">
    <source src="https://res.cloudinary.com/cd0hgkqgk/video/upload/v1539035604/audio/one35.mp3">
  </audio>
</body>

</html>
              
            
!

CSS

              
                html,
body {
  height: 100%;
}

body {
  --main-red: #ef6461;
  --main-green: #63a375;
  --main-white: #fffffa;
  --main-black: #333333;
}

#body a,
a:focus,
a:hover {
  color: var(--main-white);
}

#body {
  background-color: var(--main-red);
  text-align: center;

  align-items: center;
  color: var(--main-black);
  display: flex;
  font-family: "Comfortaa", cursive;
  justify-content: center;
}

#pomodoro-clock-inside {
  align-items: center;
  display: grid;
  justify-content: center;
}

#pomodoro-clock-inside .timer {
  grid-column: 1;
  grid-row: 1;
  width: 15rem;
}

#pomodoro-clock-inside .session-length {
  grid-column: 2;
  grid-row: 1;
  width: 12rem;
}

#pomodoro-clock-inside .break-length {
  grid-column: 3;
  grid-row: 1;
  width: 12rem;
}

#pomodoro-clock-inside .btn {
  margin: 0.2rem;
}

#pomodoro-clock-inside .fixed-width {
  width: 2.5rem;
}

#pomodoro-clock-inside .btn-set {
  padding: 0.5rem;
  margin: 0.5rem;
  border: 1px solid var(--main-white);
  border-radius: 5px;
}

#pomodoro-clock-inside .label {
  font-size: 1.1rem;
}

#pomodoro-clock-inside .time {
  font-size: 1.2rem;
  line-height: 3.5rem;
}

#pomodoro-clock-inside .timer .time {
  font-size: 2rem;
}

footer {
  margin: 1rem 0;
}

@media (max-width: 767.98px) {
  #pomodoro-clock-inside .timer {
    grid-column: 1 / span 2;
    grid-row: 1;
    width: auto;
  }

  #pomodoro-clock-inside .session-length {
    grid-column: 1;
    grid-row: 2;
    width: auto;
  }

  #pomodoro-clock-inside .break-length {
    grid-column: 2;
    grid-row: 2;
    width: auto;
  }
}

              
            
!

JS

              
                // Variable to store the intervalID of setInterval
// Used to stop the setInterval function by clearInterval
var intervalId;

const BREAK = 'Break';
const SESSION = 'Session';

const beep = document.getElementById('beep'); 

class PomodoroClock extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      timerLabel: SESSION,
      timeLeft: 1500,
      breakLength: 5,
      sessionLength: 25,
      isRunning: false
    };
    this.reset = this.reset.bind(this);
    this.decrementBreak = this.decrementBreak.bind(this);
    this.incrementBreak = this.incrementBreak.bind(this);
    this.decrementSession = this.decrementSession.bind(this);
    this.incrementSession = this.incrementSession.bind(this);
    this.countDown = this.countDown.bind(this);
    this.toggleTimer = this.toggleTimer.bind(this);
    this.startStop = this.startStop.bind(this);
    this.updateTimeLeft = this.updateTimeLeft.bind(this);
  }
  
  reset() {
    this.setState({
      timerLabel: SESSION,
      timeLeft: 1500,
      breakLength: 5,
      sessionLength: 25,
      isRunning: false
    })
    clearInterval(intervalId);
    beep.pause();
    beep.currentTime = 0;
    document.getElementById('body').style.backgroundColor = "var(--main-red)";
  }
  
  decrementBreak() {
    if (this.state.breakLength > 1) {
      this.setState((state) => ({
        breakLength: state.breakLength - 1
      }))
      this.updateTimeLeft(BREAK);
    }
  }
  
  incrementBreak() {
    if (this.state.breakLength < 60) {
      this.setState((state) => ({
        breakLength: state.breakLength + 1
      }))
      this.updateTimeLeft(BREAK);
    }
  }
  
  decrementSession() {
    if (this.state.sessionLength > 1) {
      this.setState((state) => ({
        sessionLength: state.sessionLength - 1
      }))
      this.updateTimeLeft(SESSION);
    }
  }
  
  incrementSession() {
    let currentLength = this.state.sessionLength;
    let newLength = currentLength + 1;
    if (this.state.sessionLength < 60) {
      this.setState((state)=>({
        sessionLength: state.sessionLength + 1
      }))
      this.updateTimeLeft(SESSION);
    }
  }
  
  countDown() {
      if (this.state.timeLeft > 0) {
        this.setState((state)=>({
          timeLeft: state.timeLeft - 1
        }))
      } else {
        this.toggleTimer();
        beep.play();
      }
    }
  
  toggleTimer() {
    if (this.state.timerLabel == SESSION) {
      this.setState({
        timerLabel: BREAK,
        timeLeft: this.state.breakLength * 60
      })
      document.getElementById('body').style.backgroundColor = "var(--main-green)";
    } else {
      this.setState({
        timerLabel: SESSION,
        timeLeft: this.state.sessionLength * 60
      })
      document.getElementById('body').style.backgroundColor = "var(--main-red)";
    }
  }
  
  startStop() {
    if (!this.state.isRunning) {
      this.setState({
        isRunning: true
      })
      intervalId = setInterval(this.countDown, 1000);
    } else {
      this.setState({
        isRunning: false
      })
      clearInterval(intervalId);
    }
  }
  
  updateTimeLeft(label) {
    if (label == SESSION && this.state.timerLabel == SESSION) {
      this.setState((state)=>({
        timeLeft: state.sessionLength * 60
      }))
    } else if (label == BREAK && this.state.timerLabel == BREAK) {
      this.setState((state)=>({
        timeLeft: state.breakLength * 60
      }))
    } else {
      return;
    }
  }
  
  render(){
    return (
      <div id="pomodoro-clock-inside">
        <div className="btn-set timer">
          <div>
            <div id="timer-label" className="label">{this.state.timerLabel}</div>
            <TimeLeft timeLeft={this.state.timeLeft} />
          </div>

          <div>
            <button id="start_stop" className="btn btn-light" onClick={this.startStop}>Start/Stop</button>
            <button id="reset" className="btn btn-light" onClick={this.reset}>Reset</button>
          </div>
        </div>

        <div className="btn-set session-length">
          <div id="session-label" className="label">Session Length</div>
          <div id="session-length" className="time">{this.state.sessionLength}</div>
          <div>
            <button id="session-decrement" className="btn btn-light fixed-width" onClick={this.decrementSession}>-</button>
            <button id="session-increment" className="btn btn-light fixed-width" onClick={this.incrementSession}>+</button>
          </div>
        </div>
        
        <div className="btn-set break-length">
          <div id="break-label" className="label">Break Length</div>
          <div id="break-length" className="time">{this.state.breakLength}</div>
          <div>
            <button id="break-decrement" className="btn btn-light fixed-width" onClick={this.decrementBreak}>-</button>
            <button id="break-increment" className="btn btn-light fixed-width" onClick={this.incrementBreak}>+</button>
          </div>
        </div>

      </div>
    );
  }
}

class TimeLeft extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    var timeLeft = this.props.timeLeft;
    var minutes = Math.floor((timeLeft % (60 * 60)) / 60);
    var seconds = Math.floor(timeLeft % (60));
    function mmss(m, s) {
      var m0 = ('00' + m).slice(-2);
      var s0 = ('00' + s).slice(-2);
      return m0 + ':' + s0;
    }
    return(
        <div id="time-left" className="time">{mmss(minutes, seconds)}</div>
    );
  }
}

ReactDOM.render(<PomodoroClock />, document.getElementById('pomodoro-clock'));
              
            
!
999px

Console