How to stop an Multiple SetInterval in Angular based Application

走远了吗. 提交于 2019-12-12 00:37:51

问题


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.

来源:https://stackoverflow.com/questions/41197707/how-to-stop-an-multiple-setinterval-in-angular-based-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!