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

              
                
              
            
!

CSS

              
                .session-timeout-notification
{
    background-color: #BADA55;
    border-bottom: 1px solid #000;
    color: #c01065;
    display: none;
    padding: 4px;
}
              
            
!

JS

              
                function debounce(func, threshold) {
    var timeout;

    return function() {
        var context = this,
            args = arguments;

        if (timeout) {
            clearTimeout(timeout);
        }

        timeout = setTimeout(function() {
            func.apply(context, args);

            timeout = null;
        }, threshold || 250);
    };
}

SessionTimeout = (function() {
    var session_duration = 10,
        session_timeout_id,
        logout_duration = 5,
        session_timeout_duration = (session_duration - logout_duration) * 1000,
        logout_id,
        logout_timeout_duration = logout_duration * 1000,
        events = 'keydown mousedown',
        notification_element;

    return {
        init: function() {
            var that = this;
          
            $(document).on($.trim((events + ' ').split(' ').join('.sessiontimer ')), debounce(function () {
                that.handler.apply(that);
            }, 500));
            
            this.startIdleTimeout();
        },

        handler: function() {                    
            // reset session timeout
            clearTimeout(session_timeout_id);

            this.startIdleTimeout();
        },
      
        startIdleTimeout: function () {
            var that = this;
          
            session_timeout_id = setTimeout(function () {
                that.idle.apply(that); 
            }, session_timeout_duration);
        },

        idle: function() {
            // create bar
            notification_element = this.createTimeoutNotification();

            // display bar
            notification_element.slideDown();

            // remove events
            $(document).unbind('.sessiontimer');

            // start logout timeout
            this.startLogoutTimeout();
            
            // start logout countdown
            this.startLogoutCountdown();

            // add logout events
            this.attachTimeoutNotificationEvents();
        },

        createTimeoutNotification: function() {
            var notification_element = $('<div class="session-timeout-notification">').html(function () {
                return [
                    '<strong>Hey!</strong> you will be logged off in <strong class="time">' + logout_duration + '</strong> seconds due to inactivity. ',
                    '<a href="#">Click here to continue using www.example.com</a>'
                ].join('');
            });
            
            notification_element.prependTo('body');

            return notification_element;
        },

        removeTimeoutNotification: function() {
            notification_element.slideUp(400, function() {
                $(this).remove();
            });
        },

        attachTimeoutNotificationEvents: function() {
            var that = this,
                dismiss_element = notification_element.find('a');

            dismiss_element.on('click', function() {
                // reset timer to prevent logout
                clearTimeout(logout_id);

                that.removeTimeoutNotification();

                that.init();
            });
        },

        startLogoutTimeout: function() {
            var that = this;
            
            logout_id = setTimeout(function() {
                alert('REDIRECT:LOGOUT');
                
                that.removeTimeoutNotification.apply(that);
            }, logout_timeout_duration);
        },

        startLogoutCountdown: function(time) {
            var that = this,
                time = time || logout_duration - 1;

            setTimeout(function() {
                notification_element.find('.time').html(time);

                time--;

                if (time > 0) {
                    that.startLogoutCountdown.call(that, time);
                }
            }, 1000);
        }
    };
}());

SessionTimeout.init();
              
            
!
999px

Console