Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h1>Pomodoro Timer</h1>

<div id="app"></div>
            
          
!
            
              /**
 * Add box sizing to everything
 * @link http://www.paulirish.com/2012/box-sizing-border-box-ftw/
 */
*,
*: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%;
}

/**
 * Style the timer
 */
#timer {
	font-size: 2em;
	font-weight: bold;
}
            
          
!
            
              //
// Variables
//

var countdown;


//
// Methods
//

/**
 * Play an alarm sound
 */
var alarm = function () {
    var alarm = new Audio('https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/Yodel_Sound_Effect.mp3');
    alarm.play();
};

/**
 * Add a leading 0 if none exists
 * @param  {Integer} num The number to pad
 * @return {String}      The padded number
 */
var padLeft = function (num) {
    if (num.toString().length < 2) {
        return '0' + num;
    }
    return num;
};

/**
 * Reset the timer
 */
var reset = function () {
    timer.setData({time: 1500});
};

var tick = function () {

    // Update the timer
    timer.setData({
        time: timer.getData().time - 1,
        running: true
    });

    // If the timer reaches zero, stop and sound an alarm
    if (timer.getData().time === 0) {
        stop();
        alarm();
    }

};

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

    // If the timer is at 0, reset it
    if (timer.data.time === 0) {
        reset();
    }

    // Start the timer
    tick();

    // Update the timer once a second
    countdown = window.setInterval(tick, 1000);

};

/**
 * Stop the timer
 */
var stop = function () {
    window.clearInterval(countdown);
    timer.setData({running: false});
};

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

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

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

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

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

};


//
// Inits & Event Listeners
//

// Create the timer
var timer = new Reef('#app', {
    data: {
        time: 1500,
        running: false
    },
    template: function (props) {
        var html =
            '<div id="timer">' +
                parseInt(props.time / 60, 10).toString() + ':' + padLeft(props.time % 60) +
            '</div>' +
            '<p>' +
                '<button data-action="' + (props.running ? 'stop' : 'start') + '">' + (props.running ? 'Pause' : 'Start') + '</button>' +
                '<button data-action="clear">Reset</button>' +
            '</p>';
        return html;
    }
});

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

// Listen for clicks
document.addEventListener('click', clickHandler, false);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console