Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Component({})
- export default class Question extends Vue {
- questionTimer: Timer = new Timer(30);
- startQuestionTimer() {
- this.questionTimer.startTimer();
- }
- export class Timer {
- duration;
- timeLeft: string;
- isTimerActive: boolean;
- constructor(duration: number) {
- this.duration = duration;
- }
- startTimer() {
- this.isTimerActive = true;
- let minutes, seconds, timer = this.duration;
- let interval = setInterval(() => {
- if (!this.isTimerActive) clearInterval(interval)
- minutes = Math.floor(timer / 60);
- minutes = minutes < 10 ? "0" + minutes : minutes;
- seconds = timer % 60;
- seconds = seconds < 10 ? "0" + seconds : seconds;
- this.timeLeft = minutes + ":" + seconds;
- if (--timer < 0) {
- this.isTimerActive = false;
- clearInterval(interval);
- }
- }, 1000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement