最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS: how can I call a function after certain time interval? - Stack Overflow

programmeradmin3浏览0评论

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??

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??

Share Improve this question edited Sep 28, 2021 at 10:56 Uvindu 294 bronze badges asked Dec 31, 2015 at 6:36 Tech KidTech Kid 5772 gold badges7 silver badges19 bronze badges 12
  • 2 Yes you can: developer.mozilla/en-US/docs/Web/API/WindowTimers/… – K K Commented Dec 31, 2015 at 6:38
  • 2 ng-idle module can help – Grundy Commented Dec 31, 2015 at 6:39
  • 1 use setInterval is the best idea i hope – Rahul Kamboj Commented Dec 31, 2015 at 6: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 Commented Dec 31, 2015 at 6:43
  • 1 @Tech Kid, as Grundy suggested, ng-idle is the one that takes care of inactivity and provides callbacks to work with – Arkantos Commented Dec 31, 2015 at 6:45
 |  Show 7 more ments

2 Answers 2

Reset to default 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

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

 setTimeout(function(){
    logout();
},5000)
发布评论

评论列表(0)

  1. 暂无评论