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

              
                <main>
  <div data-element="result"></div>
  <button type="button" data-element="reset" hidden>Reset timer</button>
</main>
              
            
!

CSS

              
                body {
  background: hsl(39,100%,97%);
  color: hsl(222,30%,16%);
  font-family: Arial, sans-serif;
  font-size: 2rem;
  height: 100vh;
  display: grid;
  place-items: center;
}

main {
  text-align: center;
}

main > * + * {
  margin-top: 1.5rem;
}

button {
  font: inherit;
  font-size: 1rem;
  background: transparent;
  border: 1px solid;
  padding: 0.2rem 1rem;
  cursor: pointer;
}

button:hover {
  background: hsl(39,79%,92%);
}
              
            
!

JS

              
                window.subscribers = [];

const defaultState = {
  remainingSeconds: 20
};

const state = new Proxy(defaultState, {
  set(state, key, value) {
    const oldState = {...state};

    state[key] = value;
    
    window.subscribers.forEach(callback => callback(state, oldState));

    return state;
  }
});

const resultElement = document.querySelector('[data-element="result"]');
const resetButton = document.querySelector('[data-element="reset"]');

// Function renders either the countdown seconds or an alert
// telling the user that it’s done
const renderCountdown = (currentState, oldState = null) => {
  
  // Check to see if the old state is not the same as the passed state. If it is 
  // the same, we skip out and don’t re-render
  if(oldState && oldState.remainingSeconds === currentState.remainingSeconds) {
    return;
  }
  
  // Generate formatting suffix and prefixes to improve readability
  const suffix = currentState.remainingSeconds === 1 ? '' : 's';
  const prefix = currentState.remainingSeconds < 10 ? '0' : '';
  
  // Seconds are done, so time to render the alert
  if (currentState.remainingSeconds <= 0) {
    resultElement.innerHTML = `
      <div role="alert">It’s over!</div>
    `;
    
    resetButton.removeAttribute('hidden');
    return;
  }
  
  // Render the current seconds interactively
  resultElement.innerHTML = `
    <div role="timer" aria-atomic="true"><strong>${prefix}${currentState.remainingSeconds}</strong> second${suffix} remaining</div>
  `;
  
  resetButton.setAttribute('hidden', '');
};

// Set a timer that when it runs to the end, clears itself
const runTimer = () => {
  let timer = setInterval(() => {
    state.remainingSeconds = state.remainingSeconds - 1;
    
    if (state.remainingSeconds <= 0) {
      clearInterval(timer);
    }
  }, 1000);
}

window.subscribers.push(renderCountdown);


// Run the initial state render and the starting countdown
renderCountdown(state);
runTimer();


// Set the seconds back to 20 and re-run the timer
resetButton.addEventListener('click', () => {
  state.remainingSeconds = 20;
  runTimer();
})


              
            
!
999px

Console