如何根据angular $ timeout调用事件?

时间:2017-04-03 19:52:30

标签: javascript angularjs

我有实时日志,所以暂停/恢复实时流媒体我在屏幕上添加了按钮,如果用户暂停实时日志在120秒后恢复,那么正常情况下,如果用户暂停日志它将在120秒恢复,但如果正常用户通过单击“恢复”按钮在120秒之前恢复,然后在120秒后,它将再次暂停日志,而无需用户与按钮交互。知道我在哪里实现了错误的代码吗?

main.html中

<span class="serverConnectInfo" ng-if="enableLiveRecording === true">
  <button type="button" class="btn btn-primary btn-md"  ng-click="stopLiveEvent()"><span class="glyphicon glyphicon-pause"></span>pause</button>
</span>
<span class="serverConnectInfo" ng-if="enableLiveRecording === false">
   <button type="button" class="btn btn-primary btn-md"  ng-click="stopLiveEvent()"><span class="glyphicon glyphicon-play"></span>resume (in {{counter}}s)</button>
</span>

ctrl.js

function resetTimer(){
        $scope.counter = 120;
    }
    // resetTimer();

    $scope.onTimeout = function(){
        $scope.counter--;
        if($scope.counter > 0){
            var  mytimeout = $timeout($scope.onTimeout,1000);
        }
        if($scope.counter === 0){
            $scope.stopLiveEvent();
            resetTimer();
        }
    }

    $scope.stopLiveEvent = function(){
        $scope.enableLiveRecording = !$scope.enableLiveRecording;
        if($scope.enableLiveRecording === false){
            resetTimer();
            toastr.warning('stop showing live event!');
            $scope.onTimeout();
        }
        if($scope.enableLiveRecording === true){
            toastr.info('start showing live event!');
            (function () {
                $scope.event = $scope.event.concat(tempStoreEvent);
                tempStoreEvent = [];
            })();
        }
    }

0 个答案:

没有答案