-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (43 loc) · 1.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var timerObj = document.getElementById("timer")
var startBtnObj = document.getElementById("start-btn")
var resetBtnObj = document.getElementById("reset-btn")
var isTimerRunning = false
var interval
// Reset button
resetBtnObj.addEventListener("click",function(){
// console.log("reset button clicked");
timerObj.textContent = "00:00";
})
// Start button
startBtnObj.addEventListener("click", ()=>{
// console.log("start button clicked");
if(isTimerRunning){
startBtnObj.textContent = "Start";
isTimerRunning = false
clearInterval(interval)
} else {
startBtnObj.textContent = "Stop";
isTimerRunning = true
interval = setInterval(function(){
// Handler
// console.log("hi");
// write logic to increment timer
var timerText = timerObj.textContent
var timerArr = timerText.split(":")
var minutes = Number(timerArr[0])
var seconds = Number(timerArr[1])
seconds++
if(seconds == 60){
minutes++
seconds = 0
}
if(minutes < 10){
minutes = "0" + minutes
}
if(seconds < 10){
seconds = "0" + seconds
}
timerObj.textContent = minutes + ":" + seconds
}, 1000)
}
})