简体   繁体   English

计时器未使用clearInterval()重置

[英]Timer is not getting reset using clearInterval()

This is my Timer function 这是我的计时器功能

var myTimer = setInterval(function () {
    var d = new Date();

    var seconds = d.getMinutes() * 60 + d.getSeconds();
    var fiveMin = 60 * 5;
    var timeleft = fiveMin - seconds % fiveMin;
    var result = parseInt(timeleft / 60) + ':' + timeleft % 60;
    //console.log(result);
    var timerObj = {
        timer : result
    }
    $scope.timerArray = timerObj;
    $scope.$apply();

    if (timeleft === 1) { 
        $scope.statusDetails = [];
        $scope.timeDetails();
        $scope.get_dbStatus();                  
    }

}, 1000);

This function will reset the above timer when I click a button. 当我单击一个按钮时,此功能将重置上述计时器。

$scope.refreshStatusList = function(){
    $scope.hide = true;
    $scope.$emit('LOAD');
    clearInterval(myTimer);
    $scope.statusDetails = [];
    $scope.timeDetails();
    $scope.get_dbStatus();  
};

This is my refresh button in html page upon clicking it the timer must get reset. 这是我在html页面中的刷新按钮,单击它后计时器必须重置。

<div class="col-lg-2">
    <a href="#" title="Refresh" ng-click="refreshStatusList();">
        <i class="fa fa-refresh fa-lg"></i>
    </a>
</div>

Since, you are using , You should use $interval directive, which will internally call $scope.apply() 由于使用的是 ,因此应使用$ interval指令,该指令将在内部调用$scope.apply()

Usage 用法

$scope.intervalPromise = $interval(function () {
    //Your code
}, 1000);

To clear interval 清除间隔

$interval.cancel($scope.intervalPromise);

试试这个

$scope.apply();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM