I have a function to handle logout application after user did not send request to server for a long time (it's CommonConfig.TIME_OUT_TO_LOGOUT
in the code below). I use $rootScope to store logOutInterval
as a variable. But I wonder that, if I used $rootScope as a right way.
- The
handleSuccess
is a function that handle request $http success.
Sorry for my bad English.
function handleSuccess(response) {
$anchorScroll(); // scroll to top after load API completed
if ($rootScope.logOutInterval) {
console.log("Reupdate count down to logout");
$timeout.cancel($rootScope.logOutInterval);
$rootScope.logOutInterval = undefined;
}
console.log("Start count down to logout at " + new Date());
$rootScope.logOutInterval = $timeout(function() {
console.log("Start logout at " + new Date());
$http({
method: 'get',
headers: defaultHeader(),
url: envConfig.API_HOST + CommonConfig.URI.LOG_OUT
}).then(function() {
toastr.error("Token has expired");
PageService.logOutAction();
});
}, CommonConfig.TIME_OUT_TO_LOGOUT);
return $q.resolve(response);
}