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

              
                <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

              
                //
// Variables
//

var stopwatch, timer;


//
// Methods
//

/**
 * 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
 */
var formatTime = function (time) {
    var minutes = parseInt(time / 60, 10);
    var 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';
};

/**
 * Setup the stopwatch on page load
 */
var setup = function () {

    // Create the stopwatch
    stopwatch = new Reef('#app', {
        data: {
            time: 0,
            running: false
        },
        template: function (props) {
            return `
                <div id="stopwatch">
                    ${formatTime(props.time)}
                </div>
                <p>
                    <button data-stopwatch="${props.running ? 'stop' : 'start'}">${props.running ? 'Stop' : 'Start'}</button>
                    <button data-stopwatch="reset">Reset</button>
                </p>`;
        }
    });

    // Render the stopwatch into the DOM
    stopwatch.render();

};

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

    // Render immediately
    stopwatch.data.running = true;

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

};

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

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

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

    // Check if a stopwatch action button was clicked
    var 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();
    }

};


//
// Inits & Event Listeners
//

// Setup the app
setup();

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

Console