Angular指令太慢了

时间:2015-10-05 19:45:28

标签: javascript angularjs angularjs-directive

我正在使用angular指令和工厂来实现一个简单的倒数计时器。计时器确实有效,但它在页面上呈现的速度太慢;计时器总是最后渲染。有什么方法可以加快这个过程吗?

HTML

<div time date = 'January 1, 2016 12:00:00'></div>

指令

angular.module('app')
.directive('time', ['timeUtil', '$interval', function(timeUtil, $interval) {
    return {
        restrict: 'A',
        scope: { date: '@' },

        link: function(scope, element) {

            var date = new Date(scope.date);

            $interval(function() {
                var diff;
                diff = Math.floor((date.getTime()-new Date().getTime()) / 1000);
                return element.text(timeUtil.dhms(diff));
            }, 1000);
        }
   }
}])

FACTORY

.factory('timeUtil', [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(' ');
      }
    };
  }
]);

0 个答案:

没有答案