3

My Scenario is:

I want to call the logout function after let says 30 mins automatically in my app. Is this possible to achieve this? And moreover, I want to know the time of the user's last interactivity with my application. Can anybody help me regarding this??

Uvindu
  • 29
  • 4
Tech Kid
  • 577
  • 2
  • 7
  • 19
  • 2
    Yes you can: https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout – K K Dec 31 '15 at 06:38
  • 2
    [ng-idle](http://ngmodules.org/modules/ng-idle) module can help – Grundy Dec 31 '15 at 06:39
  • 1
    use setInterval is the best idea i hope – Rahul Kamboj Dec 31 '15 at 06:39
  • 3
    @RahulKamboj, `setInteval` will call a function again and again after certain interval. If it's meant to be invoked only once, then `setTimeout` is the one. Even that won't be useful in case of Angular as that function will be executed outside AngularJS context, so I think `$timeout` is the service he needs to use. – Arkantos Dec 31 '15 at 06:43
  • 1
    @Tech Kid, as Grundy suggested, `ng-idle` is the one that takes care of inactivity and provides callbacks to work with – Arkantos Dec 31 '15 at 06:45
  • 1
    See http://stackoverflow.com/questions/16696969/angular-session-timeout-and-management – georgeawg Dec 31 '15 at 07:56
  • Thanks alot all of you for your wonderful answers. Please tell me about `ng-idle`. actullay im newbie in angular. I want to fire a function of logout if user don't any interactivity within 30 mins... @georgeawg @Grundy – Tech Kid Dec 31 '15 at 08:14
  • 1
    See this answer -- [Try ng-idle. It's simple component where you can set the timeout and warning time before the timeout is reached](http://stackoverflow.com/a/29154182/5535245). – georgeawg Dec 31 '15 at 08:23
  • @Grundy , @gergeawg , Sir, I'm getting this error now `Error: [$rootScope:inprog] http://errors.angularjs.org/undefined/$rootScope/inprog?p0=%24digest` can you please help me to turn my this `$rootscope.synchronization()` function into service or factory provider in this [Fiddle](http://plnkr.co/edit/SZ9NvTo0TE6MBo4oRRSj?p=preview).. :( – Tech Kid Dec 31 '15 at 15:26
  • 1
    @TechKid, seems this already another question – Grundy Dec 31 '15 at 15:30
  • 1
    see if [this](http://plnkr.co/edit/XHZI9iwdOCdcfq8K5QEo?p=preview) helps – Arkantos Dec 31 '15 at 16:12
  • @Arkantos My good bro, Thanks alot. This really helped me.. – Tech Kid Dec 31 '15 at 16:57

2 Answers2

9

Use below for any function that requires time out. But I will not suggest this for Logout. Better you can use browser session to logout after 30 min.

$timeout(function() {
   // your code
}, 3000); // 3 seconds
  • Thanks for your answer Sir, I'm getting this error now `Error: [$rootScope:inprog] http://errors.angularjs.org/undefined/$rootScope/inprog?p0=%24digest` can you please help me to turn my this `$rootscope.synchronization()` function into service or factory provider in this [Fiddle](http://plnkr.co/edit/SZ9NvTo0TE6MBo4oRRSj?p=preview).. :( – Tech Kid Dec 31 '15 at 15:23
4

use setTimeout() pure javascript function that can be invoked after a time of interval

 setTimeout(function(){
    logout();
},5000)
Rahul Kamboj
  • 449
  • 3
  • 14
  • Is this call a function once or call repeately after 5000 miliseconds ? – Tech Kid Dec 31 '15 at 06:47
  • 3
    In case your `logout()` changes something on scope, the this won't work. You have to wrap the call to logout in `$scope.apply()` or use `$timeout` that does this implicitly – Arkantos Dec 31 '15 at 06:52
  • @Arkantos. Thanks alot for your answer. Sir, I'm getting this error now `Error: [$rootScope:inprog] http://errors.angularjs.org/undefined/$rootScope/inprog?p0=%24digest` can you please help me to turn my this `$rootscope.synchronization()` function into service or factory provider in this [Fiddle](http://plnkr.co/edit/SZ9NvTo0TE6MBo4oRRSj?p=preview).. :( – Tech Kid Dec 31 '15 at 15:24