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

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

<script src="https://kit.fontawesome.com/e45dbc8e56.js" crossorigin="anonymous"></script>

<div id='root'></div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Arvo&display=swap')

$bgcolor: #051321
$fontcolor: darken(#E35865, 5%)

body
  margin: 0
  padding: 0
  background: $bgcolor
  font-family: 'Arvo', serif
  
#root
  background: $bgcolor
  height: 100vh
  display: flex
  align-items: center
  justify-content: center
  
#main
  display: grid
  grid-template-columns: repeat(3, 1fr)
  grid-template-rows: repeat(4, 1fr)
  column-gap: .5rem
  row-gap: .5rem
  height: 600px
  width: 800px
  background: lighten($bgcolor, 1.5%)

.griditem
  display: flex
  align-items: center
  justify-content: center
  color: $fontcolor
  background: lighten($bgcolor, 3%)
  
#timer
  grid-column: 1 / span 3
  grid-row: 2 / span 3
  font-size: 3rem
  flex-direction: column
 
#break-label, #session-label
  flex-direction: column
  font-size: 3rem
  
  .fas
    font-size: 2.45rem
    padding: .9rem
    color: darken($fontcolor, 5%)
  
  #headers
    font-size: 1.2rem
  
#buttons
  font-size: 3.5rem
  
  .far, .fas
    color: darken($fontcolor, 5%)
  
              
            
!

JS

              
                function Timer() {
  const [session, setSession] = React.useState(25)
  const [rest, setRest] = React.useState(5)
  const [sessionDuration, setSessionDuration] = React.useState(1500)
  const [restDuration, setRestDuration] = React.useState(300)
  const [active, setActive] = React.useState(true)
  const intervalID = React.useRef('')
  const audio = React.useRef()
  audio.current = document.getElementById('beep')
  
    if (sessionDuration >= 0) {
      React.useEffect(() => {
        intervalID.current = setInterval(sessionReducer, 1000)
        return () => clearInterval(intervalID.current)
      }), [sessionDuration]
    } else {
      React.useEffect(() => {
        intervalID.current = setInterval(breakReducer, 1000)
        return () => clearInterval(intervalID.current)
      }), [restDuration]}
  
  function sessionReducer() {
    if (active) {
      if (sessionDuration == 0) {
        // Trigger Alarm
      console.log(audio.current)
        if (audio.current == null) {
          audio.current = document.getElementById('beep')
        }
      audio.current.currentTime = 0;
      audio.current.play() 
      }
      if (sessionDuration >= 0) {
      setSessionDuration(sessionDuration - 1)
    }
  }
}
  
  function breakReducer() {
    if (active) {
      if (restDuration == 0) {
        console.log(audio.current)
        if (audio.current == null) {
          audio.current = document.getElementById('beep')
        }
        audio.current.currentTime = 0;
        audio.current.play()
      }
      if (restDuration > 0) {
      setRestDuration(restDuration - 1)
    } else {
      // reset Timers
      setSessionDuration(session * 60)
      setRestDuration(rest * 60)
    }
  }
}
  
  function increaseSession() {
    if (session < 60) {
      setSession(session + 1)
      setTimeout(setSessionDuration((session+1) * 60), 1000)
    }
  }
  
  function decreaseSession() {
    if (session > 1) {
      setSession(session - 1)
      setTimeout(setSessionDuration((session-1) * 60), 1000)
    }
  }
  
  function increaseRest() {
    if (rest < 60) {
      setRest(rest + 1)
      setTimeout(setRestDuration((rest+1) * 60), 1000)
    }
  }
  
  function decreaseRest() {
    if (rest > 1) {
      setRest(rest - 1)
      setTimeout(setRestDuration((rest-1) * 60), 1000)
    }
  }
  
  function pausePlay() {
    setActive(active => !active)
  }
  
  function restart() {
    clearInterval(intervalID.current)
    setActive(active => false)
    setRest(5)
    setSession(25)
    setSessionDuration(25 * 60)
    setRestDuration(5 * 60)
    audio.current.pause()
    audio.current.load()
  }
    
   let sessionMinutes = Math.floor(sessionDuration / 60)
   let sessionSeconds = sessionDuration - sessionMinutes * 60
   sessionMinutes = sessionMinutes < 10 ?  '0' + sessionMinutes: sessionMinutes
   sessionSeconds = sessionSeconds < 10 ? '0' + sessionSeconds: sessionSeconds
   let sessionClock = sessionMinutes + ':' + sessionSeconds
  
  let sessionHTML = <div id='timer' class='griditem'><div id='timer-label'>SESSION</div><p id='time-left'>{sessionClock}</p></div>

   let restMinutes = Math.floor(restDuration / 60)
   let restSeconds = restDuration - restMinutes * 60
   restMinutes = restMinutes < 10 ? '0' + restMinutes: restMinutes
   restSeconds = restSeconds < 10 ? '0' + restSeconds: restSeconds
   let restClock = restMinutes + ':' + restSeconds
      
  let breakHTML = <div id='timer' class='griditem'><div id='timer-label'>BREAK</div><p id='time-left'>{restClock}</p></div>
  
  return (
    <div id='main'>
      <div id='break-label' class='griditem'>
        <div id='headers'>BREAK DURATION</div>
        <div id='break-length'><i id='break-increment' class="fas fa-arrow-up" onClick={increaseRest}></i>{rest}<i id='break-decrement' class="fas fa-arrow-down" onClick={decreaseRest}></i></div>
      </div>
      <div id='buttons' class='griditem'>
        <i id='start_stop' class="fas fa-play" onClick={pausePlay}></i>
        &nbsp;|&nbsp;
        <i id='reset' class="fas fa-undo-alt" onClick={restart}></i></div>
      <div id='session-label' class='griditem'>
        <div id='headers'>SESSION DURATION</div>
        <div id='session-length'><i id='session-increment' class="fas fa-arrow-up" onClick={increaseSession}></i>{session}<i id='session-decrement' class="fas fa-arrow-down" onClick={decreaseSession}></i></div>
      </div>
      {sessionDuration > -1 ? sessionHTML : breakHTML}
      <audio
        preload="auto"
        id="beep"
        src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/success.mp3'
        />
    </div>
  )
}

ReactDOM.render(<Timer />, document.getElementById('root'))
console.log(document.getElementById('time-left').innerHTML)
              
            
!
999px

Console