如何查看已设置的计时器倒计时角度7

时间:2019-08-29 10:56:13

标签: javascript typescript angular7

我如何查看计时器的倒数计时?我尝试将其绑定到变量,但它以[object Object]的形式出现:

setTime(time) {
    if (this.settime >= 5 ) {
      this.currentTime = time;
      this.alerttime = this.settime;
    this.notifiy.showInfo( 'Timer set for ' + this.settime + ' Minutes train is due in ' + time + ' Minutes' , '');
    const val = this.settime * 60000;
    const source = timer(val);
    this.time = source;
    console.log(this.time); // this prints out the observbale function Ive also tryed to bind it in the subscribe method but no joy.
    const subscribe = source.subscribe(value => { this.alertTimer(); });
    this.settime = '';
    //cordova.plugins.backgroundMode.enable();
    } else {
      this.notifiy.showFailure('You cant set the timer below 5 minutes', '');
    }
  }

我不确定自己做错了什么。

2 个答案:

答案 0 :(得分:1)

简短的回答是“您不能”,因为您正在使用的实际计时器不具有此属性,例如per the documentation.

但是根据您要使用的功能,您可能希望分别跟踪实际经过的时间,为此,您可以使用“间隔”代替。

https://rxjs.dev/api/index/function/interval

假设您将timer()设置为120秒,那么它将在计划结束时执行。 然后使用单独的interval()(1秒)来跟踪经过的时间,并更新UI等,直到主计时器结束。

伪代码,显示了概念。从我的头顶开始,可能会有错别字等:

class TimerManager {

   private totalSeconds;
   private elapsedSeconds;

   private timerSub;
   private intervalSub

   // Start the timer
   public startTimer(timerLengthSeconds) {
      this.totalSeconds = timerLengthSeconds;
      var t = new timer(timerLengthSeconds);
      this.timerSub = t.subscribe(t => this.endTimer());
      var i = new interval(1000);
      this.intervalSub = i.subscribe(i => this.intervalElapsed());
   }

   // On each interval
   private intervalElapsed() {
      this.elapsedSeconds++;
      var remaining = this.totalSeconds - this.elapsedSeconds;
      console.log(this.elapsedSeconds + "s have elapsed, there are " + remaining + "s left");
   }

   // When the main timer ends
   private endTimer() {
     console.log("The time is up!!");
     this.intervalSub.unsubscribe();
   }

}

答案 1 :(得分:0)

您可以通过检查设置间隔功能来查看计时器倒数。这是一个例子。您甚至可以在Type Script中使用它。我认为这可以解决您的问题。

//120 Seconds timer...
var timeleft = 120;
var downloadTimer = setInterval(function() {
  if (timeleft <= 0) {
    clearInterval(downloadTimer);
    document.getElementById("countdown").innerHTML = "Finished";
  } else {
    document.getElementById("countdown").innerHTML = timeleft + " seconds remaining";
  }
  timeleft -= 1;
}, 1000);
<div id="countdown"></div>