How to reload the page automatically after a specified period of time, if the user is not active in angularjs

I want to reload the page every 5 minutes if the user is not active on this page (I do not want to reload the page while the user is active).

I have a code to reload the page when I click on the button.

 <button ng-click="refreshDeliveries()">
               <span>Refresh Deliveries</span>
</button>


$scope.refreshDeliveries = function () {
    $window.location.reload();
};

But I just want if the user is inactive in the last 5 minutes, the page will automatically reload in Angularjs.

thank

+4
source share
2 answers

You can use the method $intervalfrom angularJS

 function reloadPage() {
        var d = new Date();
        var curTime = d.getTime();  //n in ms

        $window.location.reload();
      }

set in 5 minutes

setIntvl = $interval(reloadPage, 300000);

cancel interval

 $interval.cancel(setIntvl);

This code works for me

and for automatic idle checking

Angularjs

0

. Angular 1.3 https://github.com/HackedByChinese/ng-idle:

(function() {
    angular.module('myApp', ['ngIdle'])
        .controller('ctrl', homeController)
        .config(function(IdleProvider, KeepaliveProvider) {
            // configure Idle settings
            IdleProvider.idle(5); // in seconds
            IdleProvider.timeout(5); // in seconds
            KeepaliveProvider.interval(2); // in seconds
        })
        .run(function(Idle) {
            // start watching when the app runs. also starts the Keepalive service by default.
            Idle.watch();
        });

    function homeController($scope, Idle) {
        $scope.message = 'Check browser console to get idle info';
        $scope.events = [];

        $scope.$on('IdleStart', function() {
            console.log('Idle Start');
            // the user appears to have gone idle
        });

        $scope.$on('IdleWarn', function(e, countdown) {
            console.log(e, countdown);          
            // follows after the IdleStart event, but includes a countdown until the user is considered timed out
            // the countdown arg is the number of seconds remaining until then.
            // you can change the title or display a warning dialog from here.
            // you can let them resume their session by calling Idle.watch()
        });

        $scope.$on('IdleTimeout', function() {
            console.log('Idle Timeout');
            // the user has timed out (meaning idleDuration + timeout has passed without any activity)
            // this is where you'd log them
            // ------You can reload the page here------
        });

        $scope.$on('IdleEnd', function() {
            console.log('Idle End');
            // the user has come back from AFK and is doing stuff. if you are warning them, you can use this to hide the dialog
        });

        $scope.$on('Keepalive', function() {
            console.log('Keep me Alive');
            // do something to keep the user session alive
        });
    }

}());
0

Source: https://habr.com/ru/post/1599232/


All Articles