javascript根据用户活动操作定时关闭通知栏插件

所属分类: 网页特效-其它&杂项    2023-11-26 04:23:17

javascript根据用户活动操作定时关闭通知栏插件 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

javascript根据用户活动操作定时关闭通知栏插件(共3个文件)

    • index.html

使用方法

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);
    };
}

var SessionTimeout = (function() {
    var session_duration = 10,
        session_timeout_id,
        logout_duration = 15, //15 minutes
        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.addEventListener(events.split(' ')[0], 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.style.display = 'block';

            // remove events
            document.removeEventListener('keydown', this.handler);
            document.removeEventListener('mousedown', this.handler);

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

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

        createTimeoutNotification: function() {
            var notification_element = document.createElement('div');
            notification_element.className = 'session-timeout-notification';
            notification_element.innerHTML = [
                '<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('');
            
            document.body.appendChild(notification_element);

            return notification_element;
        },

        removeTimeoutNotification: function() {
            notification_element.style.display = 'none';
            document.body.removeChild(notification_element);
        },

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

            dismiss_element.addEventListener('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.querySelector('.time').innerHTML = time;

                time--;

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

SessionTimeout.init();

站长提示:
1. 苦力吧素材官方QQ群:950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励2K币
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 其它&杂项

jquery鼠标拖拽拖动div元素特效DEMO演示

一款可鼠标拖拽移动的DIV元素,非常简单就可以运用了!
  其它&杂项
 5724  0

jquery自定义断点处显示在指定位置插件

可通过将元素的一个潜在父容器(在源中与匹配的数据集属性配对)设置为在指定断点处显示:block来控制元素在DIV中的位置。
  其它&杂项
 6642  0

文本框输入内容单词计数统计插件Countable

针对input及textarea文本输入框进行字符数统计,可统计:段落数、词数、字符数等。
  其它&杂项
 41065  0

jquery响应式随机切换背景颜色特效代码

一款轻量级颜色选择器插件,用于生成随机颜色,鼠标单击即可将HEX颜色代码复制到剪贴板。
  其它&杂项
 2690  0

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论