Javascript 角度倒数计时器

Javascript 角度倒数计时器,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我遇到了这个角度计时器,它对我来说非常适合。然而,当它到达倒计时日期时,我似乎无法成功地让它停止。我基本上希望它在00:00:00停止。 有什么帮助吗? 如果我正确理解了这个问题,这里的解决方案,它不是在CoffeScript中(不过它很容易适应),而是在普通的老javascript中。我评论了编辑 (function() { angular.module('app', []).directive('countdown', [ 'Util', '$interval',

我遇到了这个角度计时器,它对我来说非常适合。然而,当它到达倒计时日期时,我似乎无法成功地让它停止。我基本上希望它在00:00:00停止。 有什么帮助吗?


如果我正确理解了这个问题,这里的解决方案,它不是在CoffeScript中(不过它很容易适应),而是在普通的老javascript中。我评论了编辑

(function() {
  angular.module('app', []).directive('countdown', [
    'Util',
    '$interval',
    function(Util,
    $interval) {
      return {
        restrict: 'A',
        scope: {
          date: '@'
        },
        link: function(scope,
    element) {
          var future;
          future = new Date(scope.date);
          //----you need to keep a reference to $interval----
          var int = $interval(function() {
            var diff;
            diff = Math.floor((future.getTime() - new Date().getTime()) / 1000);
            //----just check for the value of 'diff' when it becomes 0 you have to clear $interval
            if(diff===0) {
              $interval.cancel(int);
              return element.text("BOOM!")
            }
            return element.text(Util.dhms(diff));
          },
    1000);
        }
      };
    }
  ]).factory('Util', [
    function() {
      return {
        dhms: function(t) {
          var days,
    hours,
    minutes,
    seconds;
          days = Math.floor(t / 86400);
          t -= days * 86400;
          hours = Math.floor(t / 3600) % 24;
          t -= hours * 3600;
          minutes = Math.floor(t / 60) % 60;
          t -= minutes * 60;
          seconds = t % 60;
          return [days + 'd',
    hours + 'h',
    minutes + 'm',
    seconds + 's'].join(' ');
        }
      };
    }
  ]);

}).call(this);

我相信这会对你有用,在咖啡脚本中,你应该能够把它复制到你的例子中。关键是在差异达到零时打印出来,然后取消计时器

angular.module 'app', []

.directive 'countdown', ['Util', '$interval', (Util, $interval) ->
  restrict: 'A'
  scope:
    date: '@'
  link: (scope, element) ->    
    future = new Date scope.date
    int = $interval ->
      diff = Math.floor (future.getTime() - new Date().getTime()) / 1000
      if (diff >= 0)
        element.text  Util.dhms diff
      else
        element.text  Util.dhms 0
        $interval.cancel int
    , 1000
    return
]

.factory 'Util', [ ->
  {
    dhms: (t) ->
      days = Math.floor t / 86400
      t -= days * 86400
      hours = Math.floor(t / 3600) % 24
      t -= hours * 3600
      minutes = Math.floor(t / 60) % 60
      t -= minutes * 60
      seconds = t % 60
      [ days + 'd', hours + 'h', minutes + 'm', seconds + 's' ].join ' '
  }
]

我们活不长了……不要欺骗系统。如果需要,请提供。Codepen是一个BonusID,任何给定的解决方案对您有效吗?
angular.module 'app', []

.directive 'countdown', ['Util', '$interval', (Util, $interval) ->
  restrict: 'A'
  scope:
    date: '@'
  link: (scope, element) ->    
    future = new Date scope.date
    int = $interval ->
      diff = Math.floor (future.getTime() - new Date().getTime()) / 1000
      if (diff >= 0)
        element.text  Util.dhms diff
      else
        element.text  Util.dhms 0
        $interval.cancel int
    , 1000
    return
]

.factory 'Util', [ ->
  {
    dhms: (t) ->
      days = Math.floor t / 86400
      t -= days * 86400
      hours = Math.floor(t / 3600) % 24
      t -= hours * 3600
      minutes = Math.floor(t / 60) % 60
      t -= minutes * 60
      seconds = t % 60
      [ days + 'd', hours + 'h', minutes + 'm', seconds + 's' ].join ' '
  }
]