Firstly : I'm new to angularjs or js at all. So maybe there is just a stupid mistake in my code.
So what this is about : I was trying to build a simple logout Template that is going to logout you after 10 seconds. There are also two Buttons, one for direct logout and one for cancel the logout.
My Problem : After 10 seconds the countdown doesn't stop if you just let the time pass by. So for some reason the "clearInterval" method doesn't work. If you cancel the interval by using one of the buttons it works.
So it seems like the method stills counts after the cancel and clearInterval.
<div class='container' ng-app="myAssesmentApp" ng-controller="logoutController" >
<h1>Logout</h1>
<br>
<form>
<div class='form-group'>
<p> Ur gonna loged out in <span id="countdowntimer">10 </span> seconds</p>
</div>
<button type='submit' class='btn btn-primary' ng-click ="logout()" id="close">Okay log me out</button>
<button type='submit' class='btn btn-primary' ng-click ="cancelLogout()" id="dont">i dont wanna get loged out</button>
</form>
</div>
JS:
myAssesmentApp.controller('logoutController', ['$scope', 'userService', '$state', function ($scope, userService, $state) {
$scope.timeleft = 10;
var downloadTimer = setInterval($scope.timer, 1000);
$scope.timer = function ()
{
$scope.timeleft--;
document.getElementById("countdowntimer").textContent = $scope.timeleft;
if ($scope.timeleft <= 0)
{
$scope.stop();
}
};
$scope.stop = function()
{
clearInterval(downloadTimer);
$scope.logout();
}
$scope.cancelLogout = function ()
{
clearInterval(downloadTimer);
$state.go('main.projectstate');
$scope.dynamicView = false;
};
$scope.logout = function () {
clearInterval(downloadTimer);
userService.benutzer = '';
userService.password = '';
$scope.logedIn = false;
};
}]);
So what it should do is reset and cancel the whole method after 10 seconds.
I know I'm not the best English speaker but I hope you understand my problem :) Thanks in advance!