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.

            
              <!-- hyperlinks: target="_blank" for codepen.io -->
  <audio id="alarm" class="hidden-xs-up" controls="" loop="true">
    <source src="https://github.com/gregorywong/sandbox/raw/gh-pages/freeCodeCamp/pomodoro/audio/beep.ogg" type="audio/ogg"/>
    <source src="https://github.com/gregorywong/sandbox/raw/gh-pages/freeCodeCamp/pomodoro/audio/beep.mp3" type="audio/mpeg"/>
    Your browser does not support the audio element.
  </audio>

  <div class="container h-100">
    <div class="row h-100 justify-content-center align-items-between">
      <div class="col-12 text-center">
        <h1 class="text-red title mt-2">Pomodoro</h1>
      </div>
      <div class="col-12 text-center my-auto pt-2">

        <div class="d-flex justify-content-center">
          <div class="">
            <input id="minutes" class="text-center display time-input" type="number" name="" value="25" />
          </div>
          <div class="my-auto">
            <h1 class="display">:</h1>
          </div>
          <div class="">
            <input id="seconds" class="text-center display time-input" type="number" name="" value="00" />
          </div>
        </div>

        <div class="mt-2 mt-sm-3 mt-md-4 text-center">
          <button id="start-stop-button" class="btn-mod btn-warning start-stop-button" type="button" name="button">START / STOP</button>
        </div>
      </div>

      <div class="text-center text-white mt-auto w-100 pb-4 credits">
        <p>Designed and Coded by <a class="text-warning" href="http://www.gregwong.me" target="_blank">Greg Wong</a></p>
      </div>
    </div>
  </div>
            
          
!
            
              html, body {
  position: relative;
  height: 100%;
}

body {
  background: url('https://github.com/gregorywong/sandbox/raw/gh-pages/freeCodeCamp/pomodoro/tomato.jpg') no-repeat center center fixed;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

/* so that the background doesn't affect the look of the app on very small screens */
@media (max-height: 480px) and (max-width: 675px) {
  body {
    background-size: auto 480px;
    background-attachment: scroll;
  }
}

.text-red {
  color: red;
}

.title {
  font-family: 'Monoton', cursive;
  text-shadow: 0 0 2px #000;
}

.credits {
  font-family: 'Special Elite', cursive;
  text-shadow: 0 0 2px #000;
}

.start-stop-button {
  font-family: 'Cabin Sketch', cursive;
  font-size: 1.5em;
  color: black;
  border-radius:25px;
}

.btn-mod:active {
  background-color: orange;
  color: red;
  transform: translateY(3px);
}

.btn-mod:focus {
  /* modifies default behavior of outlining buttons upon being clicked */
  outline: 0 none!important;
  box-shadow: none;
}

input {
  background: transparent;
  width: 1.8em;
  font-family: monospace;
  font-size: 4em;
  border: 0;
  border-top: 3px solid black;
  border-bottom: 3px solid black;
}

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    /* display: none; <- Crashes Chrome on hover */
    -webkit-appearance: none !important;
    margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
}

input:hover {
  color: orange;
  border-color: orange;
  text-shadow: 0 0 1px #000;
}

input:focus {
  color: yellow;
  border-color: yellow;
}

input:focus {
  outline: 0 none!important;
}

.display {
  font-family: 'Monoton', cursive;
}

            
          
!
            
              var CountDown = function(sec, tickCallback, timesUpCallback) {
  const MAX_SEC = 5999;
  var setSeconds = sec;
  validateSetSeconds(); // change it to a suitable value if necessary
  var seconds = setSeconds;
  var timerID = null;
  var tickCallback = tickCallback;
  var timesUpCallback = timesUpCallback;
  var alarmOn = false;
  const INTERVAL = 1000;

  this.set = function(sec) {
    if (!timerID) { // can't set it while it's running
      setSeconds = sec;
      validateSetSeconds();
      seconds = setSeconds;
    }
  };

  this.start = function() {
    if (!timerID) {
      // if timer has just gone off, reset it to the previously set time
      if (seconds <= 0) {
        seconds = setSeconds;
      }
      timerID = setInterval(tick, INTERVAL);
    }
  };

  this.stop = function() {
    stop();
    alarmOn = false;
  };

  this.isRunning = function() {
    return timerID != null;
  };

  this.isAlarmOn = function() {
    return alarmOn;
  };

  this.getSetSeconds = function() {
    return setSeconds;
  };

  function validateSetSeconds() {
    setSeconds = Math.floor(setSeconds); // in case a float was passed in
    if (setSeconds < 0) {
      setSeconds = 0;
    }
    else if (setSeconds > MAX_SEC) {
      setSeconds = MAX_SEC;
    }
  }

  function tick() {
    seconds--;
    if (seconds <= 0) {
      seconds = 0; // don't display negative values
      timesUpCallback();
      alarmOn = true;
      stop();
    }
    tickCallback(seconds);
  }

  function stop(){
    if (timerID) {
      clearInterval(timerID);
      timerID = null;
    }
  }
};

function getMinutesAndSeconds(sec) {
  // return seconds in MM:SS format
  var minutes = Math.floor(sec / 60);
  minutes = minutes < 10 ? '0'+ minutes : minutes;
  var seconds = sec % 60;
  seconds = seconds < 10 ? '0'+ seconds : seconds;

  return [minutes, seconds];
}

function toSeconds(minutes, seconds) {
  return minutes * 60 + seconds;
}

function getSuitableMinutes(min) {
  if (min < 0) {
    return 0;
  }
  else if (min > 99) {
    return 99;
  }
  return min;
}

function getSuitableSeconds(sec) {
  if (sec < 0) {
    return 0;
  }
  else if (sec > 59) {
    return 59;
  }
  return sec;
}

$(document).ready(function() {

  var alarm = document.getElementById("alarm");
  var alarmIntervalID;
  function flashTimer() {
    $(".display").stop().css("color", "yellow").animate({color: "black"}, 1000, stopFlash);
  }

  function stopFlash() {
    $(".display").removeAttr('style'); // this works as I do not define styles in the html file
  }

  var updateDisplay = function(sec) {
    var time = getMinutesAndSeconds(sec);
    $("#minutes").val(time[0]);
    $("#seconds").val(time[1]);
  };

  var timesUp = function() {
    alarm.play();
    flashTimer();
    alarmIntervalID = setInterval(flashTimer, 1000);
  };

  var myCountDown = new CountDown(toSeconds(25,0), updateDisplay, timesUp);

  $(".time-input").focusin(function(event) {
    alarm.pause(); // if it's not playing, this won't do anything
    alarm.load();
    clearInterval(alarmIntervalID);
    myCountDown.stop();
  });

  $(".time-input").focusout(function(event) {
    var minutes = parseInt($("#minutes").val());
    var seconds = parseInt($("#seconds").val());

    if (!minutes) {
      minutes = 0;
    }
    if (!seconds) {
      seconds = 0;
    }

    minutes = getSuitableMinutes(minutes);
    seconds = getSuitableSeconds(seconds);

    var sec = minutes * 60 + seconds;
    updateDisplay(sec);
    myCountDown.set(sec);
  });

  $("#start-stop-button").click(function(event) {
    if (myCountDown.isAlarmOn()) {
      alarm.pause();
      alarm.load();
      clearInterval(alarmIntervalID);
      myCountDown.stop();
      updateDisplay(myCountDown.getSetSeconds());
    }
    else if (myCountDown.isRunning()) {
      myCountDown.stop();
    }
    else {
      myCountDown.start();
    }
  });

});

            
          
!
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