Making a 5 min Count-down on angular that keeps resetting

Viewed 29

I'm trying to apply this countdown to my app but I don't understand why it wont work... i can set the timer to 5min but it keeps getting reseted I want it so whenever it gets to 0 it will get the user foward in the application, adding a warning to, but that i'm going to apply later

and i've called on onInit like this

   ngOnInit(): void {
        
            this.timeControl()
    }

   

     timeControl() {
        let tempoFinal = ((new Date().getTime() + (60000 * 5)))
        console.log(tempoFinal)
        let timeNow = new Date().getTime();
        console.log(timeNow)
        let tempoFinal2 = tempoFinal - timeNow;
        console.log(tempoFinal2)
        if (tempoFinal2 <= 0) {
          this.router.navigate(['../../../../testeFinal/' + this.time.id], { relativeTo: this.route })
    
        } else if (tempoFinal2 > 0) {
          tempoFinal2 = tempoFinal2 - 1000;
          let timer = document.getElementById('timer') as HTMLElement;
          timer.innerHTML = new Date(tempoFinal2).toLocaleTimeString(
            navigator.language,
            { minute: '2-digit', second: '2-digit' }
          );
          setTimeout(() => {
            this.timeControl();
          }, 1000);
        }

  }

Why does this keeps happening?

0 Answers
Related