【问题标题】:How to change countdown time in minutes instead of seconds in ng-Idle如何在 ng-Idle 中以分钟而不是秒为单位更改倒计时时间
【发布时间】:2019-06-24 17:26:27
【问题描述】:

我在我的应用程序中使用 ng-idle。在这里,警告弹出窗口在 10 秒后显示。并在弹出窗口中显示警报消息

“您的会话将在 10 秒后关闭”

。但我想在几分钟内。会议应在 5 分钟内结束。我该如何改变?我期待会话超时警报应该来,

“您的会话将在 5 分钟后结束”

谁能告诉我如何改变这个?

Plunkr

Warning-dialog.html

<div class="modal-header">
    <h3 class="modal-title" id="modal-title">{{ pc.title }}</h3>
</div>
<div idle-countdown="countdown" ng-init="countdown=5" class="modal-body" id="modal-body">
        <uib-progressbar max="10" value="10" animate="false" class="progress-striped active">You'll be logged out in {{countdown}} minutes(s).</uib-progressbar>
</div>
<div class="modal-footer">
    <button class="btn btn-primary" type="button" ng-click="pc.ok()">OK</button>
    <button class="btn btn-warning" type="button" ng-click="pc.cancel()">Cancel</button>
</div>

Timeout-dialog.html

<div class="modal-header">
    <h3 id="modal-title">You've Timed Out!</h3>
</div>
<div class="modal-body" id="modal-body">
    <p>
        You were idle too long...
    </p>
</div>

Index.html

<!doctype html>
<html ng-app="myApp">
<head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-animate.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-sanitize.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ng-idle/1.3.2/angular-idle.min.js"></script>
    <script src="app.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <section ng-controller="DemoCtrl">
        <p>
            <button type="button" class="btn btn-success" ng-hide="started" ng-click="start()">Start Demo</button>
            <button type="button" class="btn btn-danger" ng-show="started" ng-click="stop()">Stop Demo</button>
        </p>
    </section>
</body>

</html>

app.js

angular.module('myApp', ['ngAnimate', 'ngSanitize', 'ui.bootstrap', 'ngIdle']);

angular.module('myApp').config(['KeepaliveProvider', 'IdleProvider', function(KeepaliveProvider, IdleProvider) {
    IdleProvider.idle(5);
    IdleProvider.timeout(5);
    KeepaliveProvider.interval(10);

    IdleProvider.interrupt('keydown wheel mousedown touchstart touchmove scroll');
}]);

// This will check the idle status when the app starts
// angular.module('myApp').run(['Idle', function(Idle) {
//     Idle.watch();
// }]);

angular.module('myApp').controller('DemoCtrl', function ($scope, Idle, Keepalive, $uibModal) {
    var pc = this;
    pc.data = "You're Idle. Do Something!!!"; 
    $scope.started = false;

    function closeModals() {
        if( $scope.warning ) {
            $scope.warning.close();
            $scope.warning = null;
        }

        if( $scope.timeout ) {
            $scope.timeout.close();
            $scope.timeout = null;
        }
    }

    $scope.$on('IdleStart', function() {
        $scope.warning = $uibModal.open({
            animation: true,
            ariaLabelledBy: 'modal-title',
            ariaDescribedBy: 'modal-body',
            templateUrl: 'warning-dialog.html',
            controller: 'ModalInstanceCtrl',
            controllerAs: 'pc',
            size: 'sm',
            resolve: {
            data: function () {
                    return pc.data;
                }
            }
        });

        $scope.warning.result.then(function () {
            console.log('Warning modal is closing now...');            
        });
    });

    $scope.$on('IdleTimeout', function() {
        console.log('Idle timeout');        
        closeModals();    
        $scope.timeout = $uibModal.open({
            animation: true,
            ariaLabelledBy: 'modal-title',
            ariaDescribedBy: 'modal-body',
            templateUrl: 'timeout-dialog.html',
            controller: 'ModalInstanceCtrl',
            controllerAs: 'pc',
            size: 'sm',
            resolve: {
            data: function () {
                    return pc.data;
                }
            }
        });

        // Your log out code goes here
        console.log('Your log out code may goes here...');

        $scope.timeout.result.then(function () {
            console.log('Timeout modal is closing now...');            
        });
    });
    

    $scope.$on('IdleEnd', function() {
        console.log('Start closing warning modal');        
        closeModals();
    });

    $scope.start = function() {
        Idle.watch();
        $scope.started = true;
    }

    $scope.stop = function() {
        Idle.unwatch();
        $scope.started = false;
    }
})

angular.module('myApp').controller('ModalInstanceCtrl', function (data) {
  var pc = this;
  pc.title = data;
});

【问题讨论】:

    标签: javascript html css angularjs


    【解决方案1】:

    5 分钟等于 300 秒。 尝试在代码中使用IdleProvider.idle(300);。 同时更改警告对话框 html:-

    &lt;uib-progressbar max="10" value="10" animate="false" class="progress-striped active"&gt;You'll be logged out in {{(countdown/50)| number: 1}} minutes.&lt;/uib-progressbar&gt;

    【讨论】:

    • 如果我使用它,警报会显示您的会话将在 300 秒后关闭。默认以秒为单位。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多