-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstopwatch.js
70 lines (58 loc) · 1.07 KB
/
stopwatch.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
var ms = 0, s = 0, m = 0,h = 0
var timer
var display = document.querySelector('.timer-display')
var laps = document.querySelector('.laps')
function start(){
if(!timer){
timer = setInterval(run,10)
}
}
function run(){
display.innerHTML = getTimer()
ms++
if(ms === 100){
ms = 0
s++;
if (s === 60){
s=0
m++
}
if(m === 60){
m=0
h++
}
}
}
function getTimer(){
return (h<10? "0" + h :h)+":"+(m<10?"0"+m:m) + ":"+ (s<10? "0" + s:s) + ":" +(ms<10? "0" + ms :ms)
}
function pause(){
stopTimer()
}
function stopTimer(){
clearInterval(timer)
timer = false
}
function reset(){
stopTimer()
ms = 0
s = 0
m = 0
h = 0
display.innerHTML = getTimer()
}
function restart(){
if(timer)
reset()
start()
}
function lap(){
if(timer){
var li = document.createElement("li")
li.innerHTML = getTimer()
laps.appendChild(li)
}
}
function resetLap(){
laps.innerHTML = ""
}