Benjamin Li
Benjamin Li

Reputation: 1697

Angular detect no user action for 1 minute

I want to set a timer in the controller that if the user does not click any button or input any field for 1 minute, the page goes to login page.

any idea how to do that?

Upvotes: 4

Views: 3359

Answers (2)

Gonzalo Matheu
Gonzalo Matheu

Reputation: 10074

ng-idle is a module for detecting and responding to idle users.

Generally, it shows a modal when it detects inactivity and after a period of time if the user is still idle, it executes action (e.g: logout)

There is an Angular 2 version too: ng-idle2

Upvotes: 3

gotnull
gotnull

Reputation: 27214

You could use something like the following:

app.run(function($rootScope) {
  var lastDigestRun = new Date();
  $rootScope.$watch(function detectIdle() {
    var now = new Date();
    if (now - lastDigestRun > 1*60*60) {
       // login here, etc
    }
    lastDigestRun = now;
  });
});

Upvotes: 2

Related Questions