Auto logout with Angularjs based on idle user

前端 未结 10 702
面向向阳花
面向向阳花 2020-11-30 18:07

Is it possible to determine if a user is inactive and automatically log them out after say 10 minutes of inactivity using angularjs?

I was trying to avoid using jQue

10条回答
  •  醉酒成梦
    2020-11-30 18:50

    I think Buu's digest cycle watch is genius. Thanks for sharing. As others have noted $interval also causes the digest cycle to run. We could for the purpose of auto logging the user out use setInterval which will not cause a digest loop.

    app.run(function($rootScope) {
        var lastDigestRun = new Date();
        setInterval(function () {
            var now = Date.now();
            if (now - lastDigestRun > 10 * 60 * 1000) {
              //logout
            }
        }, 60 * 1000);
    
        $rootScope.$watch(function() {
            lastDigestRun = new Date();
        });
    });
    

提交回复
热议问题