Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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.

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

              
                <div id='root'></div>
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://kit.fontawesome.com/b77242129d.js" crossorigin="anonymous"></script>


              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Russo+One');
body{
  background-color:skyblue;
  font-family:Russo One;
  user-select:none;
}
*{
  margin:0;
  padding:0;
}
#wrapper {
  width:240px;
  background-color:lightgray;
  margin:10px auto;
  border-radius:6px;
  padding:8px;
  // display:flex;
  // flex-wrap:wrap;
  #description{
    font-size:40px;
    margin-top:20px;
    text-align:center;
    margin-bottom:20px;
  }
  .lengthWrapper{
    display:flex;
    flex-wrap:wrap;
    line-height:32px;
    justify-content:space-between;
    padding-left:6px;
    padding-right:6px;
    box-sizing:border-box;
    .title{
      width:124px;
    }
    .length{
      font-size:20px;
    }
    .arrowWrapper{
      display:flex;
      .arrow{
        width:19px;
        display:block;
        background-color:pink;
        text-align:center;
        box-sizing:border;
        border:1px solid gray;
        line-height:30px;
        border-radius:3px;
        cursor:pointer;
        &:hover {
          box-shadow:0 0 2px 2px;
        }
        &:hover i{
          color:white;
        }
      }
    }
  }
  #break {
    background-color:lightgreen;
    margin-bottom:10px;
  }
  #session{
    background-color:lightcoral;
    margin-bottom:20px;
  }
  #display{
    #timer-label{
      font-size:28px;
      text-align:center;
    }
    #time-left{
      font-size:28px;
      text-align:center;
      margin-bottom:6px;
    }
    #buttonWrapper{
      display:flex;
      justify-content:right;
      button{
        font-size:20px;
        display:block;
      }
      #reset{
        width:57px;
        &:hover{
          background-color:pink;
          line-height:27px;
          border:none;
          color:white;
          box-sizing:border-box;
        }
      }
      #start_stop{
        width:30px;
        line-height:23px;
        margin-right:50px;
        &:hover {
          background-color:pink;
          border:none;
          color:white;
        }
        i{
          display:block;
        }
      }
    }
  }
}
              
            
!

JS

              
                class Timer extends React.Component {
  constructor(props){
    super(props)
  }
  state={break:5, session:25, timer:false, sessionRunning:true};
  interval=null; sessionTime=this.state.session*60; breakTime=this.state.break*60;
  
  
  displayTime=(time)=>{
    let min=parseInt(time/60); let sec=time%60; var displayMin, displaySec;
    if(min<10){displayMin=`0${min}`} else {displayMin=min};
    if(sec<10){displaySec=`0${sec}`} else {displaySec=sec};
    return `${displayMin}:${displaySec}`;
  }
  
  breakUp=()=>{
    if(this.state.break<60 && this.interval===null){
      this.setState({break:this.state.break+1});
     
    }
  }
  breakDown=()=>{
    if(this.state.break>1 && this.interval===null) {this.setState({break:this.state.break-1}); }
    
  }
  sessionUp=()=>{
    let timeLeftEle=document.getElementById('time-left');
    if(this.state.session<60 && this.interval===null){this.setState({session:this.state.session+1},()=>{
      timeLeftEle.innerText=this.displayTime(this.state.session*60);
    });}
  }
  sessionDown=()=>{
    let timeLeftEle=document.getElementById('time-left');
    if(this.state.session>1 && this.interval===null){this.setState({session:this.state.session-1},()=>{
      timeLeftEle.innerText=this.displayTime(this.state.session*60);
    });}
  }
  
  startPause=()=>{
    let timeLeftEle=document.getElementById('time-left');
    let beep=document.getElementById('beep');
    let timeLeft=timeLeftEle.innerText.split(":")[0]*60+timeLeftEle.innerText.split(":")[1]*1;
    let startStop=document.getElementById('start_stop');
    if(startStop.innerHTML==='<i class="fa-solid fa-play"></i>'){
      startStop.innerHTML='<i class="fa-solid fa-pause"></i>'
    } else {
      startStop.innerHTML='<i class="fa-solid fa-play"></i>'
    }
    if(this.interval===null){
      this.interval=setInterval(()=>{
        timeLeft--;
        timeLeftEle.innerText=this.displayTime(timeLeft);
        if(timeLeft<1 && this.state.sessionRunning){
          this.setState({sessionRunning:false});
          timeLeft=this.state.break*60;
          beep.play();
        } else if (timeLeft<1 && !this.state.sessionRunning){
          this.setState({sessionRunning:true});
          timeLeft=this.state.session*60;
          beep.play();
        }
      },1000)
    } else {
      clearInterval(this.interval);
      this.interval=null;
    } 
  }
  
  resetTime=()=>{
    let startStop=document.getElementById('start_stop');
    let beep=document.getElementById('beep');
    beep.pause();
    beep.currentTime=0;
    let timeLeftEle=document.getElementById('time-left');
    let timeLeft=timeLeftEle.innerText.split(":")[0]*60+timeLeftEle.innerText.split(":")[1]*1;
    timeLeft=0;
    startStop.innerHTML='<i class="fa-solid fa-play"></i>';
    if(this.interval!==null){
      clearInterval(this.interval);
      this.interval=null;
    }
    this.setState({break:5, session:25, sessionRunning:true},()=>{
      timeLeftEle.innerText=this.displayTime(this.state.session*60);
    });
  }
  
  render(){
    return (
      <div id='wrapper'>
          <div id='description'>25+5 Clock</div>
          <div id='break' className='lengthWrapper'>
            <div className='title' id='break-label'>break length</div>
            <div className='length' id='break-length'>{this.state.break}</div>
            <div className='arrowWrapper'>
              <a className='arrow' id='break-increment' onClick={this.breakUp}><i class="fa-solid fa-arrow-up"></i></a>
              <a className='arrow' id='break-decrement' onClick={this.breakDown}><i class="fa-solid fa-arrow-down"></i></a>
            </div>
          </div>
          <div id='session' className='lengthWrapper'>
            <div className='title' id='session-label'>session length</div>
            <div className='length' id='session-length'>{this.state.session}</div>
            <div className='arrowWrapper'>
              <a className='arrow' id='session-increment' onClick={this.sessionUp}><i class="fa-solid fa-arrow-up"></i></a>
              <a className='arrow' id='session-decrement' onClick={this.sessionDown}><i class="fa-solid fa-arrow-down"></i></a>
            </div>
          </div>
          <div id='display'>
            <div id='timer-label'>{this.state.sessionRunning?'Session':'Break'}</div>
            <div id='time-left'>{this.displayTime(this.state.session*60)}</div>
            <audio id='beep' src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/audio/BeepSound.wav" preload='auto'>Cannot access the audio</audio>
            <div id='buttonWrapper'>
              <button id='start_stop' onClick={this.startPause}><i class="fa-solid fa-play"></i></button>
              <button id='reset' onClick={this.resetTime}>Reset</button>
            </div>
          </div>
      </div>
    )
  }
}
const root=ReactDOM.createRoot(document.querySelector('#root'));
root.render(<Timer />);
              
            
!
999px

Console