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. 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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Interval timer - Coderdojo Athlone</title>
</head>
<body>

<div class="main-container">

  <div class="timer-status">
    <div class="timer-overview">
      <div id="timeOverviewMessage">Finish!</div>

      <div class="box" id="elapsedInIntervalBox">
        <header>&nbsp;</header>
        <span id="elapsedInInterval"></span>s
      </div>

      <div class="box" id="elapsedInBreakIntervalBox">
        <header>Break time</header>
        <span id="elapsedInBreakInterval"></span>s
      </div>
    </div>

    <div class="timer-additional-info">
      <!--  Information about interval  -->
      <div class="box left-align">
        <header>Intervals</header>
        <div id="intervals"></div>
      </div>
      <div class="box">
        <header>Intervals done</header>
        <div id="intervalsDone"></div>
      </div>
      <div class="box">
        <header>Intervals remaining</header>
        <div id="intervalsRemaining"></div>
      </div>

      <!--  Information about time  -->
      <div class="box left-align">
        <header>Total time</header>
        <span id="totalTime"></span>s
      </div>
      <div class="box">
        <header>Total time elapsed</header>
        <span id="totalTimeElapsed"></span>s
      </div>
      <div class="box">
        <header>Total time remaining</header>
        <span id="totalTimeRemaining"></span>s
      </div>
    </div>
  </div>

  <div class="timer-controls">
    <button type="button" id="startBtn">Start</button>
    <button type="button" id="pauseBtn">Pause</button>
    <button type="button" id="stopBtn">Stop</button>
  </div>

  <div class="settings-form">
    <div class="form-line">
      <label for="intervalCountInput">Interval count:</label>
      <input type="number" id="intervalCountInput" min="1">
    </div>

    <div class="form-line">
      <label for="intervalDurationInput">Interval duration:</label>
      <input type="number" id="intervalDurationInput" min="1">
    </div>

    <div class="form-line">
      <label for="enableBreakInput">Enable break:</label>
      <input type="checkbox" id="enableBreakInput">
    </div>

    <div class="form-line" id="breakDurationInputLine">
      <label for="breakDurationInput">Break duration:</label>
      <input type="number" id="breakDurationInput" min="1">
    </div>
  </div>
</div>
<footer class="app-footer">
  <span>© 2019 - Coderdojo Athlone</span>
  <a href="https://coderdojoathlone.com/en/resources/web-resources/tutorial-interval-timer/">Open the tutorial</a>
</footer>
</body>
</html>

              
            
!

CSS

              
                html,
body {
  height: 100%;
  width: 100%;
  box-sizing: border-box;
  margin: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 1rem;
  padding: 1rem;

  background-color: #023;
  color: #E4E5E6;
  overflow: auto;
}

/*
 * General page layout
 */

h1 {
  margin-top: 0;
  text-align: center;
}

.main-container {
  display: grid;
  grid-gap: 2rem;
  grid-template-areas: "status" "controls" "settings";
  margin-bottom: 1rem;
}

.timer-status {
  grid-area: status;
}

.timer-controls {
  grid-area: controls;
}

.settings-form {
  grid-area: settings;
}

.app-footer {
  margin: 1.5rem auto;
  font-size: .8rem;
  display: flex;
  justify-content: space-between;
}

.app-footer span {
  color: #888;
}

.app-footer a,
.app-footer a:visited {
  padding-left: 1rem;
  color: #CCCCCC;
}

@media only screen and (min-width: 768px) {
  .main-container {
    max-width: 960px;
    margin: 0 auto;
    grid-template-columns: repeat(2, calc(50% - 2rem / 2));
    grid-template-areas: "status status" "settings controls";
  }
}

/*
 * Settings form
 */

.settings-form {
  border: 1px solid #CCC;
  padding: 2rem;
}

.form-line {
  display: flex;
  align-items: baseline;
  margin-bottom: 1rem;
}

.form-line:last-of-type {
  margin: 0;
}

.form-line label {
  flex-grow: 1;
  padding-right: .5rem;
}

.form-line input[type="number"] {
  width: 60px;
  text-align: right;
  font-size: 1rem;
}

/*
 * Timer controls
 */

.timer-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
  box-sizing: border-box;
  padding: 2rem;
  background-color: #356;
}

.timer-controls button {
  font-size: 1rem;
  margin: 0 1rem;
}

#startBtn {
  color: #1f9136;
  font-weight: bold;
}

#stopBtn {
  color: #990000;
}

@media only screen and (min-width: 768px) {
  .timer-controls button {
    font-size: 2rem;
    margin: 1rem;
  }
}

/*
 * Timer status
 */

.timer-status header {
  color: #888;
  margin-bottom: .5rem;
  font-size: 1rem;
}

.timer-status .box.left-align {
  text-align: left;
}

.timer-overview .box {
  flex: 0 0 50%;
  margin-bottom: 1rem;
}

.timer-overview {
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.timer-overview .box {
  text-align: center;
  font-size: 1.5rem;
}

#timeOverviewMessage,
#elapsedInInterval,
#elapsedInBreakInterval {
  font-size: 3rem;
}

#timeOverviewMessage,
#elapsedInBreakIntervalBox {
  display: none;
}

#timeOverviewMessage {
  text-align: center;
  flex: 1;
  margin-bottom: 1rem;
}

.timer-additional-info {
  display: flex;
  align-items: flex-end;
  flex-wrap: wrap;
  margin: 0 -1rem;
}

.timer-additional-info .box {
  text-align: right;
  flex: 1 0 33%;
  padding: 1rem;
  margin-bottom: .5rem;
  box-sizing: border-box;
}

              
            
!

JS

              
                (() => {

  window.addEventListener('load', onLoad);

  function onLoad() {
    setTimerSettings(3, 60, true, 15);

    initializeTimerControls();
    initializeStatusPanel();
    initializeTimerSettingsForm();
    resetTimer();
  }

  let
    formSettingsFields,
    timerControlsButtons,
    statusPanel,
    timer,
    timerSettings;

  function setTimerSettings(
    intervalCount = timerSettings.intervalCount,
    intervalDuration = timerSettings.intervalDuration,
    enableBreak = timerSettings.enableBreak,
    breakDuration = timerSettings.breakDuration
  ) {
    timerSettings = {
      intervalCount,
      intervalDuration,
      enableBreak,
      breakDuration
    };
  }

  function resetTimer() {
    timer = {
      totalTimeElapsed: 0,
      elapsedInInterval: 0,
      intervalsDone: 0,
      isBreak: false,
      isFinished: false
    };

    updateInfo();
  }

  function initializeTimerSettingsForm() {
    const oneDayInSeconds = 60 * 60 * 24;
    let lastUserSetEnableBreak = timerSettings.enableBreak;

    formSettingsFields = {
      intervalCount: document.getElementById('intervalCountInput'),
      intervalDuration: document.getElementById('intervalDurationInput'),
      enableBreak: document.getElementById('enableBreakInput'),
      breakDuration: document.getElementById('breakDurationInput'),
    };

    formSettingsFields.intervalCount.value = timerSettings.intervalCount;
    formSettingsFields.intervalDuration.value = timerSettings.intervalDuration;
    formSettingsFields.enableBreak.checked = timerSettings.enableBreak;
    formSettingsFields.breakDuration.value = timerSettings.breakDuration;

    function getNumberInBoundsOrDefault(value, min, max, def = 1) {
      const valueAsNumber = parseInt(value);
      return isNaN(valueAsNumber) ? def : Math.max(min, Math.min(valueAsNumber, max));
    }

    function setBreakDurationLineDisplay(displayed) {
      const breakDurationInputLineElt = document.getElementById('breakDurationInputLine');
      breakDurationInputLineElt.style.display = displayed ? null : 'none';
    }

    formSettingsFields.intervalCount.addEventListener('input', () => {
      const
        intervalCount = getNumberInBoundsOrDefault(formSettingsFields.intervalCount.value, 1, 999),
        hasOneInterval = intervalCount === 1,
        hasBreak = hasOneInterval ? false : lastUserSetEnableBreak;

      formSettingsFields.enableBreak.disabled = hasOneInterval === true;
      formSettingsFields.enableBreak.checked = hasBreak;

      setBreakDurationLineDisplay(hasBreak);

      setTimerSettings(intervalCount, undefined, hasBreak);
      updateInfo();
    });

    formSettingsFields.intervalDuration.addEventListener('input', () => {
      setTimerSettings(undefined, getNumberInBoundsOrDefault(formSettingsFields.intervalDuration.value, 1, oneDayInSeconds));
      updateInfo();
    });

    formSettingsFields.enableBreak.addEventListener('change', () => {
      const enableBreak = formSettingsFields.enableBreak.checked;

      lastUserSetEnableBreak = enableBreak;
      setBreakDurationLineDisplay(enableBreak);
      setTimerSettings(undefined, undefined, enableBreak);
      updateInfo();
    });

    formSettingsFields.breakDuration.addEventListener('input', () => {
      setTimerSettings(
        undefined, undefined, undefined,
        getNumberInBoundsOrDefault(formSettingsFields.breakDuration.value, 1, oneDayInSeconds)
      );
      updateInfo();
    });
  }

  function initializeTimerControls() {
    timerControlsButtons = {
      start: document.getElementById('startBtn'),
      pause: document.getElementById('pauseBtn'),
      stop: document.getElementById('stopBtn')
    };

    setTimerControlsDisabledState(false, true, true);

    timerControlsButtons.start.addEventListener('click', startTimer);
    timerControlsButtons.pause.addEventListener('click', pauseTimer);
    timerControlsButtons.stop.addEventListener('click', stopTimer);
  }

  function initializeStatusPanel() {
    statusPanel = {
      timeOverviewMessage: document.getElementById('timeOverviewMessage'),

      elapsedInIntervalBox: document.getElementById('elapsedInIntervalBox'),
      elapsedInBreakIntervalBox: document.getElementById('elapsedInBreakIntervalBox'),
      elapsedInInterval: document.getElementById('elapsedInInterval'),
      elapsedInBreakInterval: document.getElementById('elapsedInBreakInterval'),

      intervalsDone: document.getElementById('intervalsDone'),
      intervalsRemaining: document.getElementById('intervalsRemaining'),
      intervals: document.getElementById('intervals'),

      totalTimeElapsed: document.getElementById('totalTimeElapsed'),
      totalTimeRemaining: document.getElementById('totalTimeRemaining'),
      totalTime: document.getElementById('totalTime'),
    };
  }

  function setTimerControlsDisabledState(start, pause, stop) {
    timerControlsButtons.start.disabled = start;
    timerControlsButtons.pause.disabled = pause;
    timerControlsButtons.stop.disabled = stop;
  }

  function setFormDisabledState(disabled) {
    formSettingsFields.intervalCount.disabled = disabled;
    formSettingsFields.intervalDuration.disabled = disabled;
    formSettingsFields.enableBreak.disabled = disabled || timerSettings.intervalCount === 1;
    formSettingsFields.breakDuration.disabled = disabled;
  }

  function startTimer() {
    setFormDisabledState(true);
    setTimerControlsDisabledState(true, false, false);

    if (timer.isFinished) {
      resetTimer();
    }

    startTimerTick();
  }

  function pauseTimer() {
    setTimerControlsDisabledState(false, true, false);

    stopTimerTick();
  }

  function stopTimer() {
    setFormDisabledState(false);
    setTimerControlsDisabledState(false, true, true);

    stopTimerTick();
    resetTimer();
  }

  function startTimerTick() {
    timer.intervalId = setInterval(onTimerTick, 1000);
  }

  function stopTimerTick() {
    clearInterval(timer.intervalId);
  }

  function onTimerTick() {
    const
      isBreak = timerSettings.enableBreak && timer.isBreak,
      currentIntervalDuration = isBreak ? timerSettings.breakDuration : timerSettings.intervalDuration;

    if (timer.elapsedInInterval < currentIntervalDuration) {
      timer.elapsedInInterval++;
    } else {

      if (!timer.isBreak) {
        timer.intervalsDone++;
      }

      timer.isBreak = timerSettings.enableBreak ? !timer.isBreak : false;
      timer.isFinished = timer.intervalsDone === timerSettings.intervalCount;

      if (!timer.isFinished) {
        timer.elapsedInInterval = 1;
      }
    }

    if (timer.isFinished) {
      setTimerControlsDisabledState(false, true, true);
      setFormDisabledState(false);
      stopTimerTick();
    } else {
      timer.totalTimeElapsed++;
    }

    updateInfo();
  }

  function updateInfo() {
    statusPanel.timeOverviewMessage.style.display = timer.isFinished ? 'block' : null;
    statusPanel.elapsedInIntervalBox.style.display = timer.isFinished || timer.isBreak ? 'none' : null;
    statusPanel.elapsedInBreakIntervalBox.style.display = !timer.isFinished && timer.isBreak ? 'block' : null;

    if (timer.isBreak) {
      statusPanel.elapsedInBreakInterval.textContent = timer.elapsedInInterval;
    } else {
      statusPanel.elapsedInInterval.textContent = timer.elapsedInInterval;
    }

    const {
        intervalCount, intervalDuration, enableBreak, breakDuration
      } = timerSettings,
      totalTime = (intervalCount * intervalDuration) + (enableBreak ? (intervalCount - 1) * breakDuration : 0);

    statusPanel.intervalsDone.textContent = timer.intervalsDone;
    statusPanel.intervalsRemaining.textContent = timerSettings.intervalCount - timer.intervalsDone;
    statusPanel.intervals.textContent = timerSettings.intervalCount;
    statusPanel.totalTimeElapsed.textContent = timer.totalTimeElapsed;
    statusPanel.totalTimeRemaining.textContent = totalTime - timer.totalTimeElapsed;
    statusPanel.totalTime.textContent = totalTime;
  }

})();

              
            
!
999px

Console