Gyuzal R Gyuzal R - 7 months ago 23
Javascript Question

AngularJS - Detect no action for n minutes and go to default state

I am new to Angular and would appreciate your advice. I'm using

ui.router
and I configured states like this:

app.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/category");
$stateProvider
.state('category', {
url: '/category',
templateUrl: 'category.html',
controller: 'categoryController as catCtrl',
reload: true
})
.state('category.categoryDetail', {
url:'/:id',
templateUrl: 'categoryDetail.html',
controller: 'categoryDetailController as catDetailCtrl',
})

.state('category.categoryDetail.product', {
url:'/product/:prod_id/:filter_model/:current_page',
templateUrl: 'product.html',
controller: 'productController as productCtrl'
})


I need to set timeout function that checks if there is no action for 'n' minutes, then I need the website to return to the default state
'category'
. I know I could do it with
jQuery
using
setTimeout()
but I'm not sure where to embed the code.

Answer

You may use app.run .

I made an example of what you need in plunker: https://plnkr.co/edit/aHj3XeCC83hHoCcu9Vix?p=preview

Copy the code and try it locally, it will work fine.

I used this answer: auto logout with angularjs based on idle user

Comments