How to stop an Multiple SetInterval in Angular bas

2019-09-20 01:29发布

问题:

myApp.run(function ($rootScope, $interval, $timeout, $state, $localStorage) {

    load = function () { 
            cdreset(); // reset the countdown to 0
            countdown(); // call the countdown function 
     $rootScope.stayLogin = function () {
            cdreset(); // reset the countdown to 0

            alert("Proceed functioncall");

            window.NewTimerCalC = function () { alert('trisssssgered'); return false; };
            NewTimerCalCProceed();          
        };

    }

$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams, $rootScope, $state, $location) {

        NewTimerCalC();  
    });


    }); // end of run block


function NewTimerCalC(){
    var lastDigestRun = Date.now();
    var s = lastDigestRun + 2 * 60 * 1000;


    var idleCheck = setInterval(function () {
        if (currentUrl != window.location.href) {
            var now = Date.now();
            var displaytime = now - lastDigestRun > 2 * 60 * 1000;
            if (now - lastDigestRun > 1 * 60 * 1000) {
                alert("run block interval alert");
                //clearTimeout(timeclear);
                load();
            }
        }
    }, 60 * 1000);

}


// button click for staylogin.
function NewTimerCalCProceed() {
window.NewTimerCalC = function () { alert('trisssssgered'); return false; };


    var lastDigestRun = Date.now();
    var s = lastDigestRun + 2 * 60 * 1000;

    var idleCheck = setInterval(function () {
        if (currentUrl != window.location.href) {
            var now = Date.now();
            var displaytime = now - lastDigestRun > 2 * 60 * 1000;
            if (now - lastDigestRun > 1 * 60 * 1000) {
                alert("Proceed block interval alert");

                load();
            }
        }
    }, 60 * 1000);

}

when user login into the app, i have set the logout time as 2 minutes. Before 1 minutes, there is a 60 seconds countdown proceessed. when it reaches 0 seconds, redirect to login page.

InBetween the counter 60 to 1 seconds , if he clicks the proceed ,again 2 minutes need to be assigned for the current user.

Working as expected.

For the first time , user login into the app, the user get 2 minutes as logout time. Before 1 minutes he gets an warning box and countdown. there is a proceed button.when user clicks the proceed button, he is getting another 2 minutes. Before 1 minute he gets an warning message. as expected.

Problem.For the first time it working fine , the problem occur when click the proceed .

the countdown is running, when countdown reaches some seconds for example 30 seconds, another interval process running and reassign the time as 60 seconds. if user not clicking anything, the countdown reassign again and again.it is not reaching 0 seconds.

here the countdown reaches 35 secons again assigned to 60 seconds because of multiple interval.