-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstopwatch.js
More file actions
82 lines (77 loc) · 2.06 KB
/
stopwatch.js
File metadata and controls
82 lines (77 loc) · 2.06 KB
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
/*let m1=document.querySelector('.m1');
let stop=document.getElementById('.stop');
let start=document.getElementById('.start');
let reset=document.getElementById('.reset');
let milli= 0 ;
let second=0;
let minutes=0;
let timerid=null;
start.addEventListener('click',function(){
if(timerid!==null){
clearInterval(timerid);
}
timerid=setInterval(start_timer,10);
})
stop.addEventListener('click',function(){
clearInterval(timerid);
})
reset.addEventListener('click',function(){
clearInterval(timerid);
m1.innerHTML='00:00:00';
})
function start_timer(){
milli++;
if(milli==100){
milli=0;
second++;
if(second==60){
second=0
minutes++;
}
}
let msecond=milli<10?'0${milli}':milli;
let usecond=second<10?'0${milli}':second;
let uminutes=minutes<10?'0${milli}':minutes;
m1.innerHTML='${uminutes} : ${usecond} : ${msecond}';
}
wrong code and corrected code below
*/
let m1 = document.querySelector('.m1');
let stop = document.getElementById('stop');
let start = document.getElementById('start');
let reset = document.getElementById('reset');
let milli = 0;
let second = 0;
let minutes = 0;
let timerid = null;
start.addEventListener('click', function() {
if (timerid !== null) {
clearInterval(timerid);
}
timerid = setInterval(start_timer, 10);
});
stop.addEventListener('click', function() {
clearInterval(timerid);
});
reset.addEventListener('click', function() {
clearInterval(timerid);
milli = 0;
second = 0;
minutes = 0;
m1.innerHTML = '00:00:00';
});
function start_timer() {
milli++;
if (milli == 100) {
milli = 0;
second++;
if (second == 60) {
second = 0;
minutes++;
}
}
let msecond = milli < 10 ? `0${milli}` : milli;
let usecond = second < 10 ? `0${second}` : second;
let uminutes = minutes < 10 ? `0${minutes}` : minutes;
m1.innerHTML = `${uminutes} : ${usecond} : ${msecond}`;
}