如果用户在angularjs中不活动,如何在给定的时间段后自动重新加载页面

时间:2015-07-23 07:10:08

标签: javascript jquery angularjs

如果用户在该页面上没有活动,我想每5分钟重新加载一页。(我不想在用户活动时重新加载页面)。

我有一个代码,用于在点击按钮时重新加载页面。

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


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

但我只是想如果用户在过去的5分钟内没有活动,页面会自动重新加载到Angularjs。

谢谢

2 个答案:

答案 0 :(得分:0)

您可以使用angularJS

中的$interval方法
 function reloadPage() {
        var d = new Date();
        var curTime = d.getTime();  //n in ms

        $window.location.reload();
      }

在5分钟内设定

setIntvl = $interval(reloadPage, 300000);

取消间隔

 $interval.cancel(setIntvl);

此代码适用于我

和自动空闲刷新检查

  

Auto logout with Angularjs based on idle user

答案 1 :(得分: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's session alive
        });
    }

}());