-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
102 lines (99 loc) · 2.95 KB
/
app.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
let tensEl = document.getElementById('tens')
let secondsEl = document.getElementById('seconds')
let minEl = document.getElementById('min')
let buttonDiv = document.getElementById('btn-div')
// Selectinh Buttons
let startBtn = document.getElementById('start')
let stopBtn = document.getElementById('stop')
let resetBtn = document.getElementById('reset')
//creating variable
let minute = 0
let seconds = 0
let tens = 0
let timer = false;
function buttonOp(){
createStopBtn()
}
function createStopBtn(){
startBtn.remove()
let stop = document.createElement('button')
stop.textContent = "STOP"
buttonDiv.prepend(stop)
stop.onclick = function(){
stopTimer()
stop.remove()
createStartBtn()
}
}
function createStartBtn(){
let start = document.createElement('button')
start.textContent = "START"
buttonDiv.prepend(start)
start.onclick = function(){
start.remove()
createStopBtn()
startTimer()
}
}
function startTimer(){
timer = true;
stopWatch()
}
function stopTimer(){
timer = false;
clearInterval(interval);
}
function resetTimer(){
timer = false
tens = 0
minute = 0
seconds = 0
tensEl.textContent = "0" + tens
minEl.textContent = "0" + minute
secondsEl.textContent = "0" + seconds
clearInterval(interval)
}
function stopWatch(){
//if condition true below code will execute
if(timer === true){
// if timer is true then tens wil increment by 1
tens++
// if tens will be less than or equal to 9 than add 0 as a string
if(tens <= 9){
tensEl.textContent = "0" + tens
}
// if tens will be greater than or equal to 10 than remove 0 that added above
if(tens >= 10){
tensEl.textContent = tens
}
// if tens is equal to 99 than increment second by 1 and tens 0
// SECONDS
if(tens === 99){
tens = 0
seconds++
//if seconds will be less than or equal to 9 than add 0 as a string
if(seconds <= 9){
secondsEl.textContent = "0" + seconds
}
// if seconds will be greater than or equal to 10 than remove 0 that added above
if(seconds >= 10 ){
secondsEl.textContent = seconds
}
// if seconds is equal to 99 than increment minute by 1 and tens 0
// MINUTE
if(seconds === 59){
seconds = 0
minute++
//if minute will be less than or equal to 9 than add 0 as a string
if(minute <= 9){
minEl.textContent = "0" + minute
}
// if minute will be greater than or equal to 10 than remove 0 that added above
if(minute >= 10){
minEl.textContent = minute
}
}
}
interval = setTimeout(startTimer, 10)
}
}