0

I've an ionic app with auto-logout feature. When user performs any action I need to extend session timeout. I store time left in localstorage (using localStorageService).

I am able to extend time when user change screens or click buttons, but I am not sure how to do this on the rest of elements.

I simply need a way to catch any action (click on background, focus on element, button press) etc. What is the simplest way to do this? Thanks.

dease
  • 2,975
  • 13
  • 39
  • 75
  • I think it can be simpler than you are trying... you can add an event listener to see when your app goes to _background_: check [this answer](http://stackoverflow.com/a/29609534/4227915) –  Jan 07 '16 at 11:19

1 Answers1

2

You should be able to listen for a touchend event, and keep a timeout; something like this:

.controller("AppCtrl", function ($scope, $timeout) {   
    var timeoutPromise,
        $body = document.querySelector("body");

    function logout () {
        // do logout work here
    }

    function resetTimer () {
        $timeout.cancel(timeoutPromise);
        timeoutPromise = $timeout(logout, 60000); // log out after 60 seconds
    }

    ionic.on("touchstart", resetTimer, $body);
    ionic.on("touchend", resetTimer, $body);
    resetTimer();
});

a few sources:

http://ionicframework.com/docs/api/utility/ionic.EventController/ https://docs.angularjs.org/api/ng/service/$timeout

Nick Bartlett
  • 4,865
  • 2
  • 24
  • 37