片刻js反击

时间:2017-08-22 12:49:48

标签: angularjs momentjs

大家好,我正在处理一个使用时刻js的任务,所以我想做的是制作一个计数分钟,秒和小时的计数器,如toggl.com计数器我有一个名为start的输入字段和另一个我想从开始减去结束并用它替换计数器值然后计数器继续从当前值继续计数器是0:00:10start is: 01:15 PMend is : 01:30 PM我想要的要从结尾减去结果,结果是值00:15此值将计数器的当前值替换为0:00:100:15:00,然后计数器从此值开始并添加秒它作为普通计数器0:15:01等等,请检查toggl.com以了解我的意思 2 /我希望如果我将起始值的值更改为10之类的任何数字,如果时间是PM,则将其视为10 PM而不是10 AM 我的代码不能这样做

  validate(val:string) {
   let temp =this.startTime;
   let parsedTime = moment(val, "hh:mm A");
   if(parsedTime.isValid()) {
   this.startTime = parsedTime.format('hh:mm A')
   } else {
    this.startTime = "temp";
    console.log("no"); 
   }
  }

其中val开始输入字段中的输入值也是我想要的,如果我输入了一个意外的格式,它将其替换为先前的有效时间temp字符串我尝试this.startTime = this.StartTime但是它不起作用我希望验证看起来像toggl.com请访问它 enter image description here

这是我的计数器代码与我自己的工作,我不认为这是这个行为的通缉

  startPause() {// when click on play button
this.bs = moment();
this.startTime = this.bs.format('hh:mm A');
this.play = false;
this.pause = true;

let sec= 0;
let min = 0;
let hour = 0;
 this.countup = setInterval(() => {
  if(sec === 59) {
    min++;
    sec = 0;
  }else if(min > 59) {
    hour++;
    min = 0;
  }else {
    sec++;
  }
  this.timer = `${this.pad(hour)}:${this.pad(min)}:${this.pad(sec)}`;
}, 1000);

}

1 个答案:

答案 0 :(得分:0)

App.directive("timerup", function(Util,$interval,$timeout){
  return {
    scope: { start: '=' },
    require: "?ngModel",
    //template: "<input type='text' readonly class='form-control' ng-model='value' > ",
    link: function(scope, element, attrs, ngModel){

        if (!ngModel) return;

        ngModel.$render = function(){
            scope.value = ngModel.$modelValue;
        };

        var timeout = 0;
        $interval(function () {
            if((scope.start)){
                //factory('Util', [function () {
                return element.text(Util.dhms(Math.floor((new Date().getTime() - scope.start.getTime())/ 1000)));
            }
        }, 1000);

        function onTimeout() {
            //timeout = timeout +1;
            timeout++;
        }
    }
  };
});
App.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)? days + 'd': '',
                (hours)? hours + 'h':'',
                minutes + 'm',
                seconds + 's'
            ].join(' ');
        }
    };
}]);

<span timerup ng-model="model" start="start"</span>

开始=初始日期