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

              
                <!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" />
  <title>Interval Timer</title>
  <link rel="stylesheet" href="style.css" />
</head>

<body>
  <div class="pageContainer">
    <!-- <div class="themeSwitcher"></div> -->
    <div class="themeSwitcher">
      <button class="changeTheme" id="themeUp">
                    <svg
                        width="100%"
                        height="100%"
                        viewBox="0 0 1792 1792"
                        xmlns="http://www.w3.org/2000/svg"
                    >
                        <path
                            d="M1408 960v-128q0-26-19-45t-45-19h-502l189-189q19-19 19-45t-19-45l-91-91q-18-18-45-18t-45 18l-362 362-91 91q-18 18-18 45t18 45l91 91 362 362q18 18 45 18t45-18l91-91q18-18 18-45t-18-45l-189-189h502q26 0 45-19t19-45zm256-64q0 209-103 385.5t-279.5 279.5-385.5 103-385.5-103-279.5-279.5-103-385.5 103-385.5 279.5-279.5 385.5-103 385.5 103 279.5 279.5 103 385.5z"
                        />
                    </svg>
                </button>
      <button class="changeTheme" id="themeDown">
                    <svg
                        width="100%"
                        height="100%"
                        viewBox="0 0 1792 1792"
                        xmlns="http://www.w3.org/2000/svg"
                    >
                        <path
                            d="M1413 896q0-27-18-45l-91-91-362-362q-18-18-45-18t-45 18l-91 91q-18 18-18 45t18 45l189 189h-502q-26 0-45 19t-19 45v128q0 26 19 45t45 19h502l-189 189q-19 19-19 45t19 45l91 91q18 18 45 18t45-18l362-362 91-91q18-18 18-45zm251 0q0 209-103 385.5t-279.5 279.5-385.5 103-385.5-103-279.5-279.5-103-385.5 103-385.5 279.5-279.5 385.5-103 385.5 103 279.5 279.5 103 385.5z"
                        />
                    </svg>
                </button>
    </div>
    <div class="helpSelector" hidden aria-hidden="true">❔</div>
    <div class="timerContainer">
      <form class="submitTimer-form">
        <input class="timerInput minutesInput" type="text" value="01" id="timerInputMinutes" />
        <span class="timerInput timerColon">:</span>
        <input class="timerInput secondsInput" type="text" value="10" id="timerInputSeconds" />
        <button type="submit" hidden aria-hidden="true">
                        submit
                    </button>
      </form>
      <div class="incrementControls_container">
        <div class="incrementControls_button incrementControls_button-left">
          <button class="incrementer " id="increment__minutes-up">
                            <svg
                                xmlns="http://www.w3.org/2000/svg"
                                viewBox="0 0 24 24"
                            >
                                <path
                                    d="M24 10h-10v-10h-4v10h-10v4h10v10h4v-10h10z"
                                />
                            </svg>
                        </button>
          <button class="incrementer " id="increment__minutes-down">
                            <svg
                                xmlns="http://www.w3.org/2000/svg"
                                viewBox="0 0 24 24"
                            >
                                <path d="M0 10h24v4h-24z" />
                            </svg>
                        </button>
        </div>
        <div class="incrementControls_button incrementControls_button-right">
          <button class="incrementer " id="increment__seconds-up">
                            <svg
                                xmlns="http://www.w3.org/2000/svg"
                                viewBox="0 0 24 24"
                            >
                                <path
                                    d="M24 10h-10v-10h-4v10h-10v4h10v10h4v-10h10z"
                                />
                            </svg>
                        </button>
          <button class="incrementer " id="increment__seconds-down">
                            <svg
                                xmlns="http://www.w3.org/2000/svg"
                                viewBox="0 0 24 24"
                            >
                                <path d="M0 10h24v4h-24z" />
                            </svg>
                        </button>
        </div>
      </div>
      <form class="restTime">
        <input type="text" class="restTimerInput minutesInput" value="00" id="restTimerInputMinutes" />
        <span class="restTimerInput timerColon">:</span>
        <input class="restTimerInput secondsInput" type="text" value="30" id="restTimerInputSeconds" />
      </form>
      <div class="timerControl">
        <button class="timerControl__button paused" id="startButton" aria-label="Play/Pause">
                        <svg
                            width="100%"
                            height="100%"
                            viewBox="0 0 36 36"
                            version="1.1"
                            xmlns="http://www.w3.org/2000/svg"
                            xmlns:xlink="http://www.w3.org/1999/xlink"
                        >
                            <defs>
                                <path
                                    id="ytp-12"
                                    d="M11,10 L18,13.74 18,22.28 11,26 M18,13.74 L26,18 26,18 18,22.28"
                                >
                                    <animate
                                        id="animation"
                                        begin="indefinite"
                                        attributeType="XML"
                                        attributeName="d"
                                        fill="freeze"
                                        to="M11,10 L17,10 17,26 11,26 M20,10 L26,10 26,26 20,26"
                                        from="M11,10 L18,13.74 18,22.28 11,26 M18,13.74 L26,18 26,18 18,22.28"
                                        dur="0.1s"
                                        keySplines=".4 0 1 1"
                                        repeatCount="1"
                                    ></animate>
                                </path>
                            </defs>
                            <use
                                xlink:href="#ytp-12"
                                class="ytp-svg-shadow"
                            ></use>
                            <use
                                xlink:href="#ytp-12"
                                class="ytp-svg-fill"
                            ></use>
                        </svg>
                    </button>
      </div>
    </div>
    <div class="reps">
      <input class="repsInput repsInput_Number" type="text" value="2" id="repsInput" />
      <span class="repsInput repsInput_Times">X</span>
    </div>
    <div class="reset" id="resetButton"><button>Reset</button></div>
  </div>
</body>
<script src="index.js"></script>

</html>
              
            
!

CSS

              
                /* prettier-ignore */
@import url(https://fonts.googleapis.com/css?family=Roboto:400,300,100);

:root {
  --textLight: #e2e2e2;
  --textDark: #131516;

  --darkPurple: #292d3e;
  --lightPurple: #7e57c2;

  --sLightAccent: #dfdcec;
  --sLightMain: #268bd2;

  --pinkWhiteAccent: #fe3562;
  --pinkWhiteMain: #dfdcec;

  --transparentColor: #ffffff00;

  --mainColor: var(--darkPurple);
  --accentColor: var(--lightPurple);
  --textColor: var(--textLight);

  --timerSize: 320px;
  --incrementerSize: 50px;
}

html {
  touch-action: manipulation;
  box-sizing: border-box;
  background: var(--mainColor);
  color: var(--accentColor);
  font-family: "Roboto", sans-serif;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}

.timerInput {
  background: none;
  color: var(--textColor);
  border: none;
  font-size: 79px;
  justify-content: center;
  position: relative;
  padding: 0;
  z-index: 1;
}
.minutesInput {
  width: 45%;
  text-align: end;
  justify-items: stretch;
}
.secondsInput {
  width: 45%;
}

/* Block_Element-Modifier */
.pageContainer {
  display: grid;
  justify-items: center; /* center on x|x */
  align-items: center;
  height: 100vh;
  grid-template-columns: 1fr 70% 1fr;
  grid-template-rows: 1fr 50% 1fr 1fr 1fr; /* Added the last FR to give some nice bottom padding and fitting "Reset" on the page*/
  grid-template-areas:
    ".       Theme  Help"
    "Main    Main   Main"
    "Reps    Reps   Reps"
    "Reset   Reset  Reset";
}

.themeSelector {
  grid-area: Theme;
}
.helpSelector {
  grid-area: Help;
}

.submitTimer-form {
  grid-area: Time;
  display: flex;
  justify-content: center;
  /*   width:50%; */
}
.incrementControls_button-left {
  grid-area: Increment-Left;
}
.incrementControls_button-right {
  grid-area: Increment-Right;
}

.timerControl {
  grid-area: Play;
}

.restTime__secondsInput {
  text-align: center;
  font-size: 1rem;
}

.timerContainer {
  grid-area: Main;
  display: flex;
  flex-direction: column;
  padding: 1rem;
  justify-items: center; /* center on x|x */
  align-items: center;
  justify-content: space-around;
  /*   justify-content: center; */

  border: solid 10px var(--accentColor);
  border-radius: 50%;
  width: var(--timerSize);
  height: var(--timerSize);
}

.incrementControls_container {
  display: flex;
  justify-content: space-around;
  width: 100%;
}

.restTimerInput {
  padding: 0;
  color: var(--textColor);
  background: none;
  border: none;
  font-size: 1rem;
}
.restTime {
  grid-area: Rest;
  display: flex;
  text-align: center;
  font-size: 25px;
  justify-content: center;
}

.incrementer {
  border: solid 1px var(--accentColor);
  background: none;
  /* font-size: 25px; */
  fill: var(--textColor);
  border-radius: 50%;
  height: var(--incrementerSize);
  width: var(--incrementerSize);
  padding: 15px;
  /* line-height: var(--incrementerSize); */
  /* text-align: center; */
}
.incrementer > * {
  pointer-events: none;
}

.timerControl__button {
  width: 75px;
  height: 75px;
  /* background: var(--primaryColor); */
  background: none;
  border-radius: 50%;
  border: solid 3px var(--accentColor);
  fill: var(--accentColor);
}

.themeSwitcher {
  grid-area: Theme;
  width: calc(var(--timerSize) / 4);
  display: flex;
  align-items: center;
  color: var(--accentColor);
  padding: 10px 0px 10px 0px;
}

.changeTheme {
  /*     transform: rotate(-45deg); */
  fill: var(--accentColor);
  background: none;
  border: none;
  padding: 0;
}
.reps {
  grid-area: Reps;
  height: 57px;
  font-size: 48px;
  display: flex;
  align-items: baseline;
}
.repsInput {
  padding: 0;
  font-size: 48px;
  background: none;
  border: none;
  color: var(--textColor);
}
.repsInput_Number {
  text-align: right;
  width: 50%;
  /* font-size: 1rem; */
}
.repsInput_Times {
  font-size: 30px;
}
.reset {
  grid-area: Reset;
}
.reset > button {
  color: var(--textColor);
  font-size: 25px;
  background: none;
  border: none;
}

              
            
!

JS

              
                "use-strict";

// Made the timer Global. May want to look into another way.
let timer;
let isReset = true;
let isRunning = false;
let darkMode = false;
let currentTheme = 0;

// let defaultMinutesValue = '01'
// let defaultSecondsValue = '10'

let savedMinutesValue;
let savedSecondsValue;
let saveRestMinutesValue;
let saveRestSecondsValue;
let savedRepsValue;
let currentMinutesValue;
let currentSecondsValue;
let currentRestMinutesValue;
let currentRestSecondsValue;
let currentRepsValue;

const themeList = [
  { main: "darkPurple", accent: "lightPurple", text: "textLight" },
  {
    main: "sLightMain",
    accent: "sLightAccent",
    text: "textLight"
  },
  { main: "pinkWhiteMain", accent: "pinkWhiteAccent", text: "textDark" }
];

const minutesInput = document.querySelector("#timerInputMinutes");
const secondsInput = document.querySelector("#timerInputSeconds");
const restTimerInputMinutes = document.querySelector("#restTimerInputMinutes");
const restTimerInputSeconds = document.querySelector("#restTimerInputSeconds");
const repsInput = document.querySelector("#repsInput");

/**
 * Listen to start button.
 * If clicked while it's class is "running", then we pause and change class
 *
 * if it's already stopped, start it.
 */
const startButton = document.querySelector("#startButton");
startButton.addEventListener("click", () => {
  if (!isRunning) {
    // start
    clearInterval(timer);
    const timeInSeconds =
      parseInt(secondsInput.value) + parseInt(minutesInput.value) * 60;
    const restInSeconds =
      parseInt(restTimerInputSeconds.value) +
      parseInt(restTimerInputMinutes.value) * 60;
    startTimer(parseInt(timeInSeconds), parseInt(restInSeconds));
    transformPlay();
  } else {
    // pause
    clearInterval(timer);
    isRunning = false;
    transformPlay();
  }
});

const resetButton = document.querySelector("#resetButton");
resetButton.addEventListener("click", () => {
  clearInterval(timer);
  isRunning = false;
  isReset = true;
  transformPlay();
  minutesInput.value = savedMinutesValue || "00";
  secondsInput.value = savedSecondsValue || "00";
  restTimerInputMinutes.value = saveRestMinutesValue || "00";
  restTimerInputSeconds.value = saveRestSecondsValue || "00";
  repsInput.value = savedRepsValue || "1";
});

/**
 * Function that begins and holds the countdown.
 * @param {Integer} inputTime
 */
function startTimer(inputTime, restInSeconds) {
  const currentTime = Date.now();
  const goalTime = currentTime + inputTime * 1000;
  const restGoal = goalTime + (restInSeconds + 1) * 1000;

  // save the other pieces.
  if (isReset) {
    savedMinutesValue = minutesInput.value;
    currentMinutesValue = minutesInput.value;
    savedSecondsValue = secondsInput.value;
    currentSecondsValue = secondsInput.value;
    saveRestMinutesValue = restTimerInputMinutes.value;
    currentRestMinutesValue = restTimerInputMinutes.value;
    saveRestSecondsValue = restTimerInputSeconds.value;
    currentRestSecondsValue = restTimerInputSeconds.value;
    savedRepsValue = repsInput.value;
    currentRepsValue = repsInput.value;
  }

  isRunning = true;
  isReset = false;
  timer = setInterval(() => {
    let timeLeft = Math.round((goalTime - Date.now()) / 1000);

    if (timeLeft >= 0) {
      displayTimeLeft(timeLeft);
    } else {
      if (currentRepsValue > 0) {
        const restLeft = Math.round(restGoal - Date.now()) / 1000;
        displayRestLeft(restLeft);
        if (restLeft <= 0 && currentRepsValue >= 0) {
          // Decrease reps and reset everything back to before
          currentRepsValue--;
          displayRestLeft(0);
          clearInterval(timer);
          repsInput.value = currentRepsValue;
          restTimerInputMinutes.value = saveRestMinutesValue;
          restTimerInputSeconds.value = saveRestSecondsValue;
          startTimer(inputTime, restInSeconds);
        }
      } else {
        clearInterval(timer);
        isRunning = false;
      }
    }
  }, 1000);
}
/**
 * Get user input and start the timer
 */

const submitTimer = document
  .querySelector(".submitTimer-form")
  .addEventListener("submit", e => {
    e.preventDefault();
    clearInterval(timer);
    isRunning = true;
    const timeInSeconds =
      parseInt(secondsInput.value) + parseInt(minutesInput.value) * 60;
    const restInSeconds =
      parseInt(restTimerInputSeconds.value) +
      parseInt(restTimerInputMinutes.value) * 60;
    startTimer(parseInt(timeInSeconds), parseInt(restInSeconds));
  });

/**
 * Write result to the page
 */

function displayTimeLeft(time) {
  const minutesLeft = time / 60;
  let secondsLeft = time % 60;

  if (isNaN(minutesInput.value)) {
    minutesInput.value = savedMinutesValue || "00 ";
  }
  if (isNaN(secondsInput.value)) {
    secondsInput.value = savedSecondsValue || "00 ";
  }
  if (isNaN(restTimerInputMinutes.value)) {
    restTimerInputMinutes.value = saveRestMinutesValue || "00";
  }
  if (isNaN(restTimerInputSeconds.value)) {
    restTimerInputSeconds.value = saveRestSecondsValue || "00";
  }
  if (isNaN(repsInput.value)) {
    repsInput.value = savedRepsValue || "0";
  }
  if (secondsLeft < 10) {
    secondsLeft = "0" + secondsLeft;
  }

  if (minutesLeft < 10) {
    minutesInput.value = "0" + Math.floor(minutesLeft);
  } else {
    minutesInput.value = Math.floor(minutesLeft);
  }

  if (secondsLeft < 10) {
    secondsInput.value = "0" + Math.floor(secondsLeft);
  } else {
    secondsInput.value = Math.floor(secondsLeft);
  }
}
function displayRestLeft(time) {
  const minutesLeft = time / 60;
  let secondsLeft = time % 60;

  if (minutesLeft < 10) {
    restTimerInputMinutes.value = "0" + Math.floor(minutesLeft);
  } else {
    restTimerInputMinutes.value = Math.floor(minutesLeft);
  }

  if (secondsLeft < 10) {
    restTimerInputSeconds.value = "0" + Math.floor(secondsLeft);
  } else {
    restTimerInputSeconds.value = Math.floor(secondsLeft);
  }
}

/**
 * Listen for the incrementing buttons, and change input numbers as needed
 */
document.querySelectorAll(".incrementer").forEach(e => {
  document.addEventListener("click", incrementTime);
});

function incrementTime(e) {
  switch (e.target.id) {
    case "increment__seconds-up": {
      if (secondsInput.value < 9) {
        secondsInput.value = parseInt(secondsInput.value) + 1;
        secondsInput.value = "0" + secondsInput.value;
      } else {
        secondsInput.value = parseInt(secondsInput.value) + 1;
      }
      break;
    }
    case "increment__seconds-down": {
      if (secondsInput.value > 0) {
        if (secondsInput.value <= 10) {
          secondsInput.value = parseInt(secondsInput.value) - 1;
          secondsInput.value = "0" + secondsInput.value;
        } else {
          secondsInput.value = parseInt(secondsInput.value) - 1;
        }
      }
      break;
    }
    case "increment__minutes-up": {
      if (minutesInput.value < 9) {
        minutesInput.value = parseInt(minutesInput.value) + 1;
        minutesInput.value = "0" + minutesInput.value;
      } else {
        minutesInput.value = parseInt(minutesInput.value) + 1;
      }
      break;
    }
    case "increment__minutes-down": {
      if (minutesInput.value > 0) {
        if (minutesInput.value <= 10) {
          minutesInput.value = parseInt(minutesInput.value) - 1;
          minutesInput.value = "0" + minutesInput.value;
        } else {
          minutesInput.value = parseInt(minutesInput.value) - 1;
        }
      }
      break;
    }
    default:
      break;
  }
}

function transformPlay() {
  const svg_animation = document.querySelector("#animation");
  const pause =
    "M11,10 L18,13.74 18,22.28 11,26 M18,13.74 L26,18 26,18 18,22.28";
  const play = "M11,10 L17,10 17,26 11,26 M20,10 L26,10 26,26 20,26";
  svg_animation.setAttribute("to", isRunning ? play : pause);
  svg_animation.setAttribute("from", isRunning ? play : pause);
  svg_animation.beginElement();
}
document.querySelector("#themeUp").addEventListener("click", changeThemeUp);
document.querySelector("#themeDown").addEventListener("click", changeThemeDown);

function changeThemeDown() {
  if (currentTheme === 0) {
    currentTheme = themeList.length - 1;
  } else {
    currentTheme--;
  }
  changeTheme(themeList[currentTheme]);
}

function changeThemeUp() {
  if (currentTheme == themeList.length - 1) {
    currentTheme = 0;
  } else {
    currentTheme++;
  }

  changeTheme(themeList[currentTheme]);
}

function changeTheme({ accent, main, text }) {
  document.documentElement.style.setProperty("--mainColor", `var(--${main})`);
  document.documentElement.style.setProperty(
    "--accentColor",
    `var(--${accent})`
  );
  document.documentElement.style.setProperty("--textColor", `var(--${text})`);
}

              
            
!
999px

Console