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

              
                let {signal, component} = reef;

// Create signals
let time = signal(0);
let running = signal(false);

// Placeholder variable for the timer
let timer;

/**
 * Format the time in seconds into hours, minutes, and seconds
 * @param  {Number} val  The time in seconds
 * @return {String}      The time in hours, minutes, and seconds
 */
function formatTime (val) {
    let minutes = parseInt(val / 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 ' : '') + (val % 60) + 's';
}

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

    // Render immediately
    running.value = true;

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

}

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

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

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

// Create the component
component('#app', template, {events: {start, stop, reset}});
              
            
!
999px

Console