72

I'm using Angular 5.

I want to know how can I start timing when a 'play' button is pressed, in order to know how much time has passed since I clicked.

I'd also like to know if it's possible to stop the timer and then be able to continue with the same time before.

I've finally solved my question with Pardeep Jain answer. Athough it wasn't exactly what I was looking for. I didn't want a countdown, I wanted to count the duration. Here is the code I've used at the end:

time: number = 0;
interval;

startTimer() {
  this.play = true;
  this.interval = setInterval(() => {
    this.time++;
  },1000)
}

pauseTimer() {
  this.play = false;
  clearInterval(this.interval);
}
Samuel
  • 816
  • 1
  • 7
  • 13

2 Answers2

129

You can simply use setInterval to create such timer in Angular, Use this Code for timer -

timeLeft: number = 60;
  interval;

startTimer() {
    this.interval = setInterval(() => {
      if(this.timeLeft > 0) {
        this.timeLeft--;
      } else {
        this.timeLeft = 60;
      }
    },1000)
  }

  pauseTimer() {
    clearInterval(this.interval);
  }
    
<button (click)='startTimer()'>Start Timer</button>
<button (click)='pauseTimer()'>Pause</button>

<p>{{timeLeft}} Seconds Left....</p>

#Working Example

#Another way using Observable timer like below -

import { timer } from 'rxjs';

observableTimer() {
    const source = timer(1000, 2000);
    const abc = source.subscribe(val => {
      console.log(val, '-');
      this.subscribeTimer = this.timeLeft - val;
    });
  }

<p (click)="observableTimer()">Start Observable timer</p> {{subscribeTimer}}

Working Example

For more information read here

Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
0

This may be overkill for what you're looking for, but there is an npm package called marky that you can use to do this. It gives you a couple of extra features beyond just starting and stopping a timer. You just need to install it via npm and then import the dependency anywhere you'd like to use it. Here is a link to the npm package: https://www.npmjs.com/package/marky

An example of use after installing via npm would be as follows:

import * as _M from 'marky';

@Component({
 selector: 'app-test',
 templateUrl: './test.component.html',
 styleUrls: ['./test.component.scss']
})

export class TestComponent implements OnInit {
 Marky = _M;
}

constructor() {}

ngOnInit() {}

startTimer(key: string) {
 this.Marky.mark(key);
}

stopTimer(key: string) {
 this.Marky.stop(key);
}

key is simply a string which you are establishing to identify that particular measurement of time. You can have multiple measures which you can go back and reference your timer stats using the keys you create.

bitW0lf
  • 147
  • 6
  • 6
    Why use extra package just to use a simple timer? – Pardeep Jain May 22 '18 at 05:21
  • 3
    Gives him the versatility to use it anywhere without needed to create a whole new service or separate helper util. Also allows you to store multiple different timer stats to revisit if he needed to resume the timer. Not trying to say your answer is wrong, just trying to provide another option for him. – bitW0lf May 22 '18 at 12:32
  • 1
    Another example of an unnecessary package, with something that is already supported in angular with setInterval. The "versatility" is already supported by angular setInterval. Providing another option for him without explaining, you remove control & add another package to manage. https://medium.com/@alexjamesdunlop/unnecessary-packages-b3623219d86 – Alex Dunlop Dec 17 '20 at 21:38