【问题标题】:Angular 2 multiple countdown pipeAngular 2 多倒计时管道
【发布时间】:2017-12-25 11:11:29
【问题描述】:

我正在寻找创建 2/4 角倒计时管道。

我当然可以进行单独的倒计时,但是如何创建多个倒计时呢?

我想要以下输入:

<span [time]="unix timestamp here">Countdown will count here</span>

例如:

<span [time]="1500503492">Countdown will count here</span>
<span [time]="15005034392">Countdown will count here</span>
<span [time]="1500503492">Countdown will count here</span>

无论我有多少,我怎样才能做到这一点?

到目前为止,我尝试的只是单个倒计时,如下所示:

time = 30;
setInterval(() => {
  this.currentTime = new Date().getTime();
  if (this.time > 0) {
    this.time = this.time - 1;
  }
}, 1000);

{{ time}}

【问题讨论】:

    标签: javascript angular countdown angular2-directives countdownlatch


    【解决方案1】:

    我认为您正在寻找 Component 而不是 PipeDirective

    这个组件应该做你想做的事:

    import { Component, Input, ChangeDetectionStrategy, ChangeDetectorRef, OnDestroy } from '@angular/core';
    
    @Component({
      selector: 'ngx-countdown',
      template: '{{ displayTime }}',
      changeDetection: ChangeDetectionStrategy.OnPush
    })
    export class CountdownComponent implements OnDestroy {
      private _time: number;
      private _timing: number = 1000;
      private _interval;
    
      @Input()
      public set time(value: string | number) {
        this._time = parseInt(value as string, 10);
        this._startTimer();
      }
    
      @Input()
      public set timing(value: string | number) {
        this._timing = parseInt(value as string, 10);
        this._startTimer();
      }
    
      @Input()
      public format: string = '{dd} days {hh} hours {mm} minutes {ss} seconds';
    
      public get delta() {
        let date = new Date();
        return Math.max(0, Math.floor((this._time - date.getTime()) / 1000));
      }
    
      public get displayTime() {
        let days, hours, minutes, seconds, delta = this.delta, time = this.format;
    
        days = Math.floor(delta / 86400);
        delta -= days * 86400;
        hours = Math.floor(delta  / 3600) % 24;
        delta -= hours * 3600;
        minutes = Math.floor(delta  / 60) % 60;
        delta -= minutes * 60;
        seconds = delta % 60;
    
        time = time.replace('{dd}', days);
        time = time.replace('{hh}', hours);
        time = time.replace('{mm}', minutes);
        time = time.replace('{ss}', seconds);
    
        return time;
      }
    
      constructor(private _changeDetector: ChangeDetectorRef) { }
    
      ngOnDestroy() {
        this._stopTimer();
      }
    
      private _startTimer() {
        if(this.delta <= 0) return;
        this._stopTimer();
        this._interval = setInterval(() => {
          this._changeDetector.detectChanges();
          if(this.delta <= 0) {
            this._stopTimer();
          }
        }, this._timing);
      }
    
      private _stopTimer() {
        clearInterval(this._interval);
        this._interval = undefined;
      }
    }
    

    您可以输入您想要计入的时间作为 unix 时间戳,还可以定义显示倒计时的格式。

    这是一个使用上述组件的example

    【讨论】:

    • 谢谢!。但是在我离开当前视图后,我在倒计时时遇到错误(因为订阅的所有内容都被破坏了)。我怎样才能破坏所有不再存在的倒计时?因为detectChanges
    • 更新了代码并添加了一个 ngOnDestroy 生命周期钩子,它会在组件销毁时停止计时器。
    • 太棒了。所以如果我想在计数器达到一个值时停止它,我该怎么做呢?
    猜你喜欢
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 2017-03-11
    • 2023-03-25
    相关资源
    最近更新 更多