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

jquery高亮显示异常中的.NET插件特效

netStack.js是一个用于.NET的最小jQuery语法高亮显示插件,使您能够使用自己的CSS样式高亮显示异常信息(堆栈跟踪)。
  其它&杂项
 210  

jquery带记录节奏的交互式打鼓乐器动画

可开启打鼓节奏记录,可循环播放;还可键盘代替鼠标击打,非常的强大!
  其它&杂项
 476  

jquery页面底部cookie操作使用通知消息栏插件

一款网页底部cookie使用通知同意特效,鼠标点击同意接受cookie后,记录到cookie当中,再次访问不再显示。
  其它&杂项
 216  

jquery页面加载完所有img图片后再执行代码特效

一款图片成功加载后再执行插件,可在页面DOM中的所有图片(含img或背景图片)成功加载后运行代码。
  其它&杂项
 205  

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

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