javascript - 计时器为 0 时发出警报消息 :0

标签 javascript angularjs timer

我有一个指令,我正在计算计时器倒计时,如下所示

<div stop-watch name="candidateInfo.name" time-of-interview="candidateInfo.dateOfInterview" class="stop-watch"></div>

    'use strict';
angular.module('iSourcingApp.tpModule')
    .directive('stopWatch', function($state) {
        return {
            restrict: 'A',
            replace: false,
            scope: {
                name: "=",
                timeOfInterview: "=",
                onSend: '&',
                startInterview:'&',
                viewPage:"="
            },
            controller: function($scope, $interval) {debugger
                $scope.getTimeRemaining = function(endtime) {
                    $scope.t[$scope.name].total = Date.parse(endtime) - Date.parse(new Date());
                    $scope.t[$scope.name].seconds = Math.floor(($scope.t[$scope.name].total / 1000) % 60);
                    $scope.t[$scope.name].minutes = Math.floor(($scope.t[$scope.name].total / 1000 / 60) % 60);
                    $scope.t[$scope.name].hours = Math.floor(($scope.t[$scope.name].total / (1000 * 60 * 60)) % 24);
                    $scope.t[$scope.name].days = Math.floor($scope.t[$scope.name].total / (1000 * 60 * 60 * 24));
                }
                $scope.initializeClock = function(endtime) {debugger
                    $scope.t = {};
                    $scope.t[$scope.name] = {};
                    $scope.updateClock = function() {
                        $scope.getTimeRemaining(endtime);
                        $scope.t[$scope.name].hours = ('0' + $scope.t[$scope.name].hours).slice(-2);
                        $scope.t[$scope.name].minutes = ('0' + $scope.t[$scope.name].minutes).slice(-2);
                        $scope.t[$scope.name].seconds = ('0' + $scope.t[$scope.name].seconds).slice(-2);

                        if ($scope.t[$scope.name].total == 0) {
                            console.log($scope.t[$scope.name].total);
                            $interval.cancel($scope.timeOfInterview);
                        }
                    }
                    $scope.updateClock();
                    $scope.timeinterval = $interval($scope.updateClock, 1000);
                }
                $scope.initializeClock($scope.timeOfInterview);
            },
            templateUrl: function() {
                var tpl = $state.current.name;
                return './tpModule/views/' + tpl + '.html';
            }
        };
    });

但这里我无法停止计时器,尽管倒计时达到 0

这里的 timeOfInterview 位于 ng-repeat 和 t[$scope.name] 中,我用来将不同的计时器绑定(bind)到不同的候选人。

这是下面的绑定(bind)模板示例

<div ng-show="viewPage" class=" btn active interview-timer" ng-click="startInterview();onSend()">
<p>Interview Starts in
    <span class="days" ng-bind="t[name].days"></span>:
    <span class="hours" ng-bind="t[name].hours"></span>:
    <span class="minutes" ng-bind="t[name].minutes"></span>:
    <span class="seconds" ng-bind="t[name].seconds"></span>
</p>

感谢任何帮助。

最佳答案

我认为你应该打电话

$interval.cancel($scope.timeinterval);

而不是

 $interval.cancel($scope.timeOfInterview);

关于javascript - 计时器为 0 时发出警报消息 :0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38765469/

相关文章:

python - 现实生活过程的异步、多线程模拟

jakarta-ee - 使用有状态 ejb 的 TimerService 的解决方法

javascript - 如何访问 'this' 控件的 clientID 或者在 jscript 中执行 Bootstrap 的 .popover()

javascript - 无法从字符串集合中找到 href 标签

Javascript 多计时器倒计时

javascript - Angular 复选框未绑定(bind)到模型

angularjs - ng-model 未使用 Chrome 操作系统上的屏幕键盘更新

javascript - 消除 Javascript 中的按键延迟

javascript - 使用字符串数组创建多级对象,将数组解析为JS对象

angularjs - 构建 AngularJS 包后,如何使用 NodeJS 作为后端请求服务器来部署 AngularJS