-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
58 lines (51 loc) · 1.14 KB
/
main.js
File metadata and controls
58 lines (51 loc) · 1.14 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
var stopwatch = document.getElementById("stopwatch");
var startBtn = document.getElementById("start-btn");
var timeoutId = null;
var ms = 0;
var sec = 0;
var min = 0;
// Function to start stopwatch
function start(flag){
if (flag){
startBtn = true;
}
timeoutId = setTimeout(function(){
ms = parseInt(ms);
sec = parseInt(sec);
min = parseInt(min);
ms++;
if (ms == 100){
sec = sec + 1;
ms = 0;
}
if (sec == 60){
min == min + 1;
sec = 0;
}
if (ms < 10){
ms = '0' + ms;
}
if (sec < 10){
sec = '0' + sec;
}
if (min < 10){
min = '0' + min;
}
stopwatch.innerHTML = min + ':' + sec + ':' + ms;
start();
}, 10); // delay time 10 ms
}
// Function to pause stopwatch
function pause(){
clearTimeout(timeoutId);
startBtn.disabled = false;
}
// Function to reset stopwatch
function reset(){
ms = 0;
sec = 0;
min = 0;
clearTimeout(timeoutId);
stopwatch.innerHTML = '00:00:00';
startBtn.disabled = false;
}