Session countdown not working after re watch angularjs ng-idle

AngularJS ng-idle Session Timeout – Warning and Extend Session Issue

I’m implementing a session timeout feature with a warning dialog and the option to extend the session using AngularJS with ng-idle. My code uses a session timeout of 2 minutes with a warning that pops up 1 minute before the session expires. The user has the option to extend the session by clicking a button, and if they don’t, the session times out.

Code Implementation

Configuration

Here is the AngularJS module configuration code, including the session timeout settings and Keepalive for session sliding:

var app = angular.module('HrSystem',
    ['ui.bootstrap',
        'ngSanitize',
        'ngIdle']
).config(['$uibTooltipProvider', 'KeepaliveProvider', 'IdleProvider', function ($uibTooltipProvider, KeepaliveProvider, IdleProvider) {
    const sessionTimeout = 120; // 2 min
    const reminderTime = 60; // 1 min

    IdleProvider.idle(sessionTimeout - reminderTime);
    IdleProvider.timeout(reminderTime);
    IdleProvider.autoResume('notIdle');

    // Configure keepalive
    const keepAliveInterval = (sessionTimeout - reminderTime) * 0.75;
    KeepaliveProvider.interval(keepAliveInterval);
    KeepaliveProvider.http(serviceBase + '/dashboard/SessionSliding');
}]);

Session Controller

This is the controller that manages the session timeout functionality, including event handling for idle start, idle end, and idle timeout:

app.controller('SessionController', ['$scope', 'Idle', 'Keepalive', '$uibModal', '$sce', 'DashboardServices', function ($scope, Idle, Keepalive, $uibModal, $sce, DashboardServices) {
    $scope.isTimeout = false;
    
    $scope.closeModals = function () {
        if ($scope.warning) {
            $scope.warning.close();
            $scope.warning = null;
        }

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

    $scope.$on('IdleStart', function () {
        $scope.closeModals();
        $scope.isTimeout = false;
        $scope.warning = $uibModal.open({
            templateUrl: 'warning-dialog.html',
            windowClass: 'to_pop',
            backdrop: 'static',
            keyboard: false,
            scope: $scope,
        });
    });

    $scope.$on('IdleEnd', function () {
        $scope.closeModals();
    });

    $scope.$on('IdleTimeout', function () {
        $scope.isTimeout = true;
    });

    $scope.$on('$destroy', function () {
        $scope.closeModals();
        Idle.unwatch(); // Stop watching when the controller is destroyed
    });

    $scope.continueWithSession = function () {
        $scope.isSessionExtending = true;
        let Service = DashboardServices.SessionSliding();
        Service.then(function (response) {
            Idle.watch();
            $scope.closeModals();
            $scope.isSessionExtending = false;
        });
    };
}]);

Warning Dialog

Here is the code for the warning dialog that pops up when the user is idle, offering them an option to continue the session:

<script type="text/ng-template" id="warning-dialog.html"> <div class="modal-body">
        <div class="session_tbox" ng-hide="isTimeout">
            <p>Your online session will expire in</p>
            <p idle-countdown="countdown" ng-bind-html="convertHMS(countdown)">{{countdown}}</p>
            <p>Please click Continue to stay connected</p>
            <button ng-click="continueWithSession()" ng-disabled="isSessionExtending">Continue</button>
        </div>
        <div class="session_tbox" ng-show="isTimeout">
            <p>Your session has expired</p>
            <p>Due to inactivity</p>
            <div>
                @Html.ActionLink("OK", "LogoutUser", "Login", null, {
                    @class = "btn_blank_bg_pop",
                    onclick = "$(this).css('pointer-events', 'none'); $(this).css('opacity', '.5'); $(this).text('Processing...');"
                })
            </div>
        </div>
    </div> </script>

Web.config

    <sessionState timeout="2" />
    <authentication mode="Forms">
        <forms name="userLogin" path="/" timeout="2" loginUrl="~/Login/Index" slidingExpiration="true" />
    </authentication>

MVC Controllar

  [HttpGet]
  public ActionResult SessionSliding()
  {
      return Json( true, JsonRequestBehavior.AllowGet);
  }

Issue

The session timeout and warning dialog work fine, but I’m experiencing the following issues:

  • Clicking “Continue” to extend the session works fine the first time. However, when extending the session a second time, the countdown triggers the IdleTimeout event before reaching zero. It could happen 10, 20, or 18 seconds before reaching zero, indicating the session has expired.
  • The IdleTimeout event triggers prematurely in some cases, leading to unexpected session expiration.

Does anyone have any suggestions on how to address these issues?
What could be causing that type of issue, and why might the session not be extending as expected?

The countdown should reach zero before the session timeout is triggered.

enter image description here

enter image description here

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật