Advertisement
Guest User

Untitled

a guest
Aug 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Component({})
  2. export default class Question extends Vue {
  3.     questionTimer: Timer = new Timer(30);
  4.  
  5.     startQuestionTimer() {
  6.         this.questionTimer.startTimer();
  7.     }
  8.  
  9. export class Timer {
  10.     duration;
  11.     timeLeft: string;
  12.     isTimerActive: boolean;
  13.  
  14.     constructor(duration: number) {
  15.         this.duration = duration;
  16.     }
  17.  
  18.     startTimer() {
  19.         this.isTimerActive = true;
  20.         let minutes, seconds, timer = this.duration;
  21.         let interval = setInterval(() => {
  22.             if (!this.isTimerActive) clearInterval(interval)
  23.             minutes = Math.floor(timer / 60);
  24.             minutes = minutes < 10 ? "0" + minutes : minutes;
  25.             seconds = timer % 60;
  26.             seconds = seconds < 10 ? "0" + seconds : seconds;
  27.             this.timeLeft = minutes + ":" + seconds;
  28.  
  29.             if (--timer < 0) {
  30.                 this.isTimerActive = false;
  31.                 clearInterval(interval);
  32.             }
  33.         }, 1000);
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement