【问题标题】:How to reload a page automatically after a given period of time if user is not active in angularjs如果用户在 angularjs 中不活动,如何在给定时间段后自动重新加载页面
【发布时间】:2015-07-23 07:10:07
【问题描述】:

如果用户在该页面上未处于活动状态,我希望每 5 分钟重新加载一次页面。(我不想在用户处于活动状态时重新加载页面)。

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

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


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

但我只想如果用户在过去 5 分钟内不活跃,页面将在 Angularjs 中自动重新加载。

谢谢

【问题讨论】:

  • 你可以使用github.com/HackedByChinese/ng-idle的ng-idle库
  • 嗨@AjayKumar,我看到了这个,但我无法理解,你能有一个简单的例子或演示。所以我很容易理解。谢谢
  • 查看我的回答,它可能对您也有帮助/跨度>

标签: javascript jquery angularjs


【解决方案1】:

你可以使用 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

【讨论】:

    【解决方案2】:

    此代码示例可能对您有所帮助。它使用 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
            });
        }
    
    }());
    

    【讨论】:

      猜你喜欢
      • 2011-06-06
      • 1970-01-01
      • 2013-09-23
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      相关资源
      最近更新 更多