简体   繁体   English

如何清除Angular中的setInterval ID?

[英]How to clear setInterval ID in Angular?

I am building a basic timer in Angular. For this I have two button, one of them starts the timer and the other stops it.我正在 Angular 中构建一个基本计时器。为此,我有两个按钮,其中一个启动计时器,另一个停止计时器。 I am implementing setInterval with 1000ms delay.我正在以 1000 毫秒的延迟实现 setInterval。 But when I press the stop button, the timer stops but the timeoutId is not cleared.但是当我按下停止按钮时,计时器停止但 timeoutId 没有被清除。 I want to prevent the user not to start another timer when the first one doesnt finish.我想防止用户在第一个计时器未完成时不启动另一个计时器。 I use - if (this.timeoutId) return;我使用 - if (this.timeoutId) return; - But I am not able to start it again as I stop it. - 但我无法在停止时再次启动它。 The question is how I can prevent the user to start new timer when the timer started.问题是我如何防止用户在计时器启动时启动新计时器。 And how can I start it again when I stop it.以及当我停止它时如何重新启动它。

//game-control-component.ts //游戏控制组件.ts

import { Component, OnInit, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-game-control',
  templateUrl: './game-control.component.html',
  styleUrls: ['./game-control.component.css'],
})
export class GameControlComponent implements OnInit {

@Output() onIncrement: EventEmitter<number> = new EventEmitter();
timer: number = 0;
timeoutId!: ReturnType<typeof setTimeout>;

constructor() {
  console.log(this.timeoutId);
}

increment() {
  this.timer++;
}

startTimer() {
  if (this.timeoutId) return;
  this.timeoutId = setInterval(() => {
    this.increment();
    this.onIncrement.emit(this.timer);
  }, 1000);
}

stopTimer() {
  clearInterval(this.timeoutId);
  console.log(this.timeoutId);
}

ngOnInit(): void {
  console.log(this.timeoutId);
 }

}

//game-control-component-html //游戏控制组件html

<div class="row mt-5">
  <div class="col-12">
    <div class="button-group d-flex justify-content-evenly">
      <button (click)="startTimer()" class="btn btn-primary">Start</button>
      <button (click)="stopTimer()" class="btn btn-danger">Stop</button>
    </div>
  <h1 class="mt-5" style="text-align: center">{{ timer }}</h1>
  </div>

There is no inbuilt way for achieving this.没有内置的方法可以实现这一点。

The only possible solution is to clear the interval variable once you clear the interval object.唯一可能的解决方案是在清除间隔 object 后清除间隔变量。

stopTimer() {
  clearInterval(this.timeoutId);
  this.timeoutId = null; // Clear the timeoutId
}

And now the start timer works as expexted.现在开始计时器按预期工作。 You are checking whether the timeoutId exist inside the start timer function您正在检查启动计时器 function 中是否存在 timeoutId

startTimer() {
  if (this.timeoutId) return; // Function will exist from here if timeoutId exist
  this.timeoutId = setInterval(() => {
    this.increment();
    this.onIncrement.emit(this.timer);
  }, 1000);
}
    // clear the timer once you click the stop button set 0 the timer variable .
stopTimer() {
      clearInterval(this.timeoutId);
      this.timer= 0; // Clear the timer
    }
    startTimer() {
      if (this.timeoutId) return; // remove this one
      this.timeoutId = setInterval(() => {
        this.increment();
        this.onIncrement.emit(this.timer);
      }, 1000);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM