显示倒计时时间表jquery会话超时 [英] show countdown time form jquery session timeout

查看:125
本文介绍了显示倒计时时间表jquery会话超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 https://github.com/travishorn/jquery-sessionTimeout 我希望在我的项目中使用的jquery会话超时。

I've found https://github.com/travishorn/jquery-sessionTimeout this jquery session timeout that I want to use in my project.

它在后台工作正常,但我想在文本中显示倒计时时间?

It looks work fine in background, altough I want to show countdown time in a text?

如何显示计时器,或者除了这个之外你们还有其他建议吗?

How can I show the timer, or do you guys have any other suggestion except this?

(function( $ ){
    jQuery.sessionTimeout = function( options ) {
        var defaults = {
            message      : 'Your session is about to expire.',
            keepAliveUrl : '/keep-alive',
            redirUrl     : '/timed-out',
            logoutUrl    : '/log-out',
            warnAfter    : 900000, // 15 minutes
            redirAfter   : 1200000 // 20 minutes
        };

        // Extend user-set options over defaults
        var o = defaults,
                dialogTimer,
                redirTimer;

        if ( options ) { o = $.extend( defaults, options ); }

        // Create timeout warning dialog
        $('body').append('<div title="Session Timeout" id="sessionTimeout-dialog">'+ o.message +'</div>');
        $('#sessionTimeout-dialog').dialog({
            autoOpen: false,
            width: 400,
            modal: true,
            closeOnEscape: false,
            open: function() { $(".ui-dialog-titlebar-close").hide(); },
            buttons: {
                // Button one - takes user to logout URL
                "Log Out Now": function() {
                    window.location = o.logoutUrl;
                },
                // Button two - closes dialog and makes call to keep-alive URL
                "Stay Connected": function() {
                    $(this).dialog('close');

                    $.ajax({
                        type: 'POST',
                        url: o.keepAliveUrl
                    });

                    // Stop redirect timer and restart warning timer
                    controlRedirTimer('stop');
                    controlDialogTimer('start');
                }
            }
        });

        function controlDialogTimer(action){
            switch(action) {
                case 'start':
                    // After warning period, show dialog and start redirect timer
                    dialogTimer = setTimeout(function(){
                        $('#sessionTimeout-dialog').dialog('open');
                        controlRedirTimer('start');
                    }, o.warnAfter);
                    break;

                case 'stop':
                    clearTimeout(dialogTimer);
                    break;
            }
        }

        function controlRedirTimer(action){
            switch(action) {
                case 'start':
                    // Dialog has been shown, if no action taken during redir period, redirect
                    redirTimer = setTimeout(function(){
                        window.location = o.redirUrl;
                    }, o.redirAfter - o.warnAfter);
                    break;

                case 'stop':
                    clearTimeout(redirTimer);
                    break;
            }
        }

        // Begin warning period
        controlDialogTimer('start');
    };
})( jQuery );

这是工作样本=> http://jsfiddle.net/xHEF9/515/

推荐答案

试试这个

var SessionTime = 10000;
var tickDuration = 1000;

var myInterval = setInterval(function() {
    SessionTime = SessionTime - tickDuration
    $("label").text(SessionTime);
}, 1000);

var myTimeOut = setTimeout(SessionExpireEvent, SessionTime);

$("input").click(function() {
    clearTimeout(myTimeOut);
    SessionTime = 10000;
    myTimeOut = setTimeout(SessionExpireEvent, SessionTime);
});

function SessionExpireEvent() {
    clearInterval(myInterval);

    alert("Session expired");
}

查看此 jsFiddle 示例。

这篇关于显示倒计时时间表jquery会话超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆