首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将时钟同步到角度2及以上版本的自定义时间

如何将时钟同步到角度2及以上版本的自定义时间
EN

Stack Overflow用户
提问于 2020-07-14 07:38:26
回答 1查看 334关注 0票数 0

下面的代码显示了一个模拟时钟和一个数字时钟,用来读取Date()中的时间。HTML输入时间字段用于捕获来自用户的时间。一旦,用户选择自定义时间并单击同步按钮,则时钟应同步到自定义时间并保持滴答。我尝试过在Sync()中使用setInterval()做一些事情,但没有得到想要的结果,也想不出其他方法来实现它。请提供一些想法来做到这一点。Link to Stackblitz

代码语言:javascript
复制
   import { Component, ElementRef, ViewChild, AfterViewInit } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <button (click)="Sync()" id=sync>Sync</button>
    
    <div class="clock">
      <div class="analog-clock">
        <div class="hour hand" [ngStyle]="hourHandStyle"></div>
        <div class="minute hand" [ngStyle]="minuteHandStyle"></div>
        <div class="second hand" [ngStyle]="secondHandStyle"></div>
        <div class="center-circle"></div>
        <input type="time" id="appt" name="appt" step=1 value="">
      </div>
      <div class="digital-clock">{{format(hour)}}:{{format(minute)}}:{{format(second)}}</div>
      <span>{{inputValue}}</span>
    </div>
    <div>{{inputValue}}</div>
  `,
  styles: [`
    @import url('https://fonts.googleapis.com/css?family=Source+Code+Pro');

    .analog-clock {
      position: relative;
      margin: 100px auto 0;
      width: 200px;
      height: 200px;
      background-color: aliceblue;
      border-radius: 50%;
    }

    .hand {
      position: absolute;
      left: 50%;
      width: 1px;
      height: 100px;
      transform-origin: 100% 100%;
    }

    .hour {
      background-color: #f44336;
    }

    .minute {
      background-color: #3f51b5;
    }

    .second {
      background-color: #9e9e9e;
    }

    .center-circle {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate3d(-50%, -50%, 0);
      width: 12px;
      height: 12px;
      background-color: black;
      border-radius: 50%;
    }

    .digital-clock {
      position: absolute;
      top: 350px;
      left: 50%;
      transform: translate3d(-50%, 0, 0);
      font-size: 2em;
      font-family: 'Source Code Pro', monospace;
    }
  `]
})
export class AppComponent implements AfterViewInit {

  hourHandStyle; 
  minuteHandStyle; 
  secondHandStyle; 

  isRunning = true;
  timerId: any;

  date: Date;
  hour: number = 0;
  minute: number = 0;
  second: number = 0;

  hh1: any; mm1: any; ss1: any;

  ngAfterViewInit() {
    this.timerId = this.getTime();
  }

  animateAnalogClock() {
    this.hourHandStyle = { transform: `translate3d(-50%, 0, 0) rotate(${(this.hour * 30) + (this.minute * 0.5) + (this.second * (0.5 / 60))}deg)` };
    
    this.minuteHandStyle = { transform: `translate3d(-50%, 0, 0) rotate(${(this.minute * 6) + (this.second * 0.1)}deg)` };
    
    this.secondHandStyle = { transform: `translate3d(-50%, 0, 0) rotate(${this.second * 6}deg)` };
  }

  getTime() {
    return setInterval(() => {
      this.date = new Date();
      this.hour = this.date.getHours();
      this.minute = this.date.getMinutes();
      this.second = this.date.getSeconds();

      this.animateAnalogClock();
    }, 1000);
  }

  format(num: number) {
    return (num + '').length === 1 ? '0' + num : num + '';
  }

  setTime()
  {
    let inputValue = (<HTMLInputElement>document.getElementById("appt")).value;
   console.log(inputValue);
   this.hh1= inputValue.substr(0,2);
   this.mm1 = inputValue.substr(3,2);
   this.ss1 = inputValue.substr(6,2);
   this.hour = + this.hh1;
   this.minute= + this.mm1;
   this.second= + this.ss1;
  }

  Sync() {

     this.setTime();
      this.animateAnalogClock();
      
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-17 22:12:49

我遵循了以下链接中的解决方案,它对我很有效。

How to create a Javascript clock with custom input time?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62885924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档