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

              
                <h1>Stopwatch</h1>

<div id="app">Loading...</div>
              
            
!

CSS

              
                /**
 * Add box sizing to everything
 * @link http://www.paulirish.com/2012/box-sizing-border-box-ftw/
 */
/* line 38, src/sass/components/_normalize.scss */
*,
*:before,
*:after {
	box-sizing: border-box;
}

/**
 * Layout
 */
body {
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
	font-size: 112.5%;
	margin-left: auto;
	margin-right: auto;
	max-width: 40em;
	width: 88%;
}

#stopwatch {
	font-size: 2em;
	font-weight: bold;
} 
              
            
!

JS

              
                let {store, component} = reef;

// Create reactive store
let data = store({
    time: 0,
    running: false
});

// Placeholder variable for the timer
let timer;

/**
 * Format the time in seconds into hours, minutes, and seconds
 * @param  {Number} time The time in seconds
 * @return {String}      The time in hours, minutes, and seconds
 */
function formatTime (time) {
    let minutes = parseInt(time / 60, 10);
    let hours = parseInt(minutes / 60, 10);
    if (minutes > 59) {
        minutes = minutes % 60;
    }
    return (hours > 0 ? hours + 'h ' : '') + (minutes > 0 || hours > 0 ? minutes + 'm ' : '') + (time % 60) + 's';
}

/**
 * Start the stopwatch
 */
function start () {

    // Render immediately
    data.running = true;

    // Update the timer once a second
    timer = window.setInterval(function () {
        data.time++;
    }, 1000);

}

/**
 * Stop the stopwatch
 */
function stop () {
    window.clearInterval(timer);
    data.running = false;
}

/**
 * Reset the stopwatch
 */
function reset () {
    data.time = 0;
    stop();
}

/**
 * Handle click events
 */
function clickHandler (event) {

    // Check if a stopwatch action button was clicked
    let action = event.target.getAttribute('data-stopwatch');
    if (!action) return;

    // If it's the start button, start
    if (action === 'start') {
        start();
        return;
    }

    // If it's the stop button, stop
    if (action === 'stop') {
        stop();
        return;
    }

    // If it's the stopwatch button, reset
    if (action === 'reset') {
        reset();
    }

}

/**
 * Create template
 * @return {[type]} [description]
 */
function template () {
    return `
        <div id="stopwatch">
            ${formatTime(data.time)}
        </div>
        <p>
            <button data-stopwatch="${data.running ? 'stop' : 'start'}">${data.running ? 'Stop' : 'Start'}</button>
            <button data-stopwatch="reset">Reset</button>
        </p>`;
}

// Create the component
component('#app', template);

// Listen for clicks
document.addEventListener('click', clickHandler);
              
            
!
999px

Console