1

I have a timer running in my app and i also have a part where i warn the user about something using a confirm dialog pop up. I noticed my timer script pauses any time the confirm box pops up and continues when the confirm box goes off.

Here is my timer script;

startCountDown(seconds){
   this.counter = seconds;
   this.interval = setInterval(() => {
      this.remainingTime = this.counter;
      this.counter--;
      if (this.counter < 0 ) {
         clearInterval(this.interval);
      } 
   }, 1000);
}

If a confirm box is called like this:

if ( confirm("Do you wish to continue") )

the script pauses and continues when the confirm box goes away. I am aware this is the normal way it should function but i was thinking there would be a way to prevent the pop up from pausing the timer script. Any help would be appreciated. Thanks.

emmaakachukwu
  • 515
  • 1
  • 7
  • 16

1 Answers1

0

Have you tried moving the timer code to a service class and invoking the method from there, Since service runs in the background it is independent of the execution in the component file.So even if the modal popup is called, the time will keep running.

Kallam
  • 169
  • 1
  • 2
  • 12
  • okay. but how do i get to access the remainingTime variable if i move the code to a service – emmaakachukwu Aug 01 '19 at 15:46
  • Yes, If the variable declared in service is public, You can access the variable just like you can access a method from the service. Please check this stack overflow answer [link](https://stackoverflow.com/questions/34572005/persisting-and-accessing-values-globally-in-multiple-components-in-angular-2) – Kallam Aug 02 '19 at 09:05