Guest User

Untitled

a guest
Jan 25th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Timer {
  2.     constructor(root, myTime) {
  3.  
  4.         this.el = {
  5.             minutes: root.querySelector(".timer-part-minutes"),
  6.             seconds: root.querySelector(".timer-part-seconds"),
  7.             control: root.querySelector(".timer-btn-control"),
  8.             reset: root.querySelector(".timer-btn-reset")
  9.         };
  10.  
  11.  
  12.         this.interval = null;
  13.         this.remainingSeconds = myTime;
  14.  
  15.         const minutes = Math.floor(this.remainingSeconds / 60);
  16.         const seconds = this.remainingSeconds % 60;
  17.  
  18.         this.el.minutes.textContent = minutes.toString().padStart(2, "0");
  19.         this.el.seconds.textContent = seconds.toString().padStart(2, "0");
  20.  
  21.         this.el.control.addEventListener("click", () => {
  22.             if (this.interval == null) {
  23.                 this.start();
  24.             }
  25.             else {
  26.                 this.stop();
  27.             }
  28.         });
  29.  
  30.         this.el.reset.addEventListener("click", () => {
  31.             alarm.pause();
  32.             alarm.load();
  33.             this.stop();
  34.             this.remainingSeconds = myTime;
  35.             this.updateInterfaceTime();
  36.         });
  37.     }
  38.  
  39.     ringAlarm() {
  40.         alarm.play();
  41.     }
  42.  
  43.     updateInterfaceTime() {
  44.         const minutes = Math.floor(this.remainingSeconds / 60);
  45.         const seconds = this.remainingSeconds % 60;
  46.  
  47.         this.el.minutes.textContent = minutes.toString().padStart(2, "0");
  48.         this.el.seconds.textContent = seconds.toString().padStart(2, "0");
  49.     }
  50.  
  51.     updateInterfaceControls() {
  52.         if (this.interval == null) {
  53.             this.el.control.innerHTML = `<span class="material-icons">play_arrow</span>`;
  54.             this.el.control.classList.add("timer-btn-start");
  55.             this.el.control.classList.remove("timer-btn-stop");
  56.         }
  57.         else {
  58.             this.el.control.innerHTML = `<span class="material-icons">pause</span>`;
  59.             this.el.control.classList.add("timer-btn-stop");
  60.             this.el.control.classList.remove("timer-btn-start");
  61.         }
  62.     }
  63.  
  64.     start() {
  65.         if (this.remainingSeconds == 0) return;
  66.         this.interval = setInterval(() => {
  67.             this.remainingSeconds--;
  68.             this.updateInterfaceTime();
  69.  
  70.             if (this.remainingSeconds == 0) {
  71.                 this.stop();
  72.                 this.ringAlarm();
  73.             }
  74.         }, 1000);
  75.  
  76.         this.updateInterfaceControls();
  77.     }
  78.  
  79.     stop() {
  80.         clearInterval(this.interval);
  81.  
  82.         this.interval = null;
  83.  
  84.         this.updateInterfaceControls();
  85.     }
  86.  
  87. }
  88.  
  89. new Timer(
  90.     document.querySelector(".timer"), 5
  91. );
Advertisement
Add Comment
Please, Sign In to add comment