-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
59 lines (46 loc) · 1.66 KB
/
script.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
const timer = document.getElementById('timer');
const startButton = document.getElementById('start');
const stopButton = document.getElementById('stop');
const resetButton = document.getElementById('reset');
let startTime = 0;
let elapsedTime = 0;
let timerInterval;
function startTimer(){
startTime = Date.now() - elapsedTime
timerInterval = setInterval( ()=> {
elapsedTime = Date.now() - startTime
timer.textContent = formatTimer(elapsedTime);
}, 10)
startButton.disabled = true;
stopButton.disabled = false;
}
function stopTimer(){
clearInterval(timerInterval);
startButton.disabled = false;
stopButton.disabled = true;
}
function resetTimer(){
clearInterval(timerInterval);
elapsedTime = 0;
timer.textContent = "00:00:00";
startButton.disabled = false;
stopButton.disabled = false;
}
function formatTimer(elapsedTime){
const hours = Math.floor(elapsedTime / (1000 * 60 * 60));
const minutes = Math.floor((elapsedTime % (1000 * 60 * 60)) / (1000 * 60))
const seconds = Math.floor((elapsedTime % (1000 * 60)) / 1000);
const mseconds = Math.floor((elapsedTime % 1000) / 10);
return (
(hours ? (hours > 9 ? hours : "0" + hours) : "00")
+ ":" +
(minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00")
+ ":" +
(seconds ? (seconds > 9 ? seconds : "0" + seconds) : "00")
+ "." +
(mseconds > 9 ? mseconds : "0" + mseconds));
}
startButton.addEventListener('click', startTimer)
stopButton.addEventListener('click', stopTimer)
resetButton.addEventListener('click', resetTimer)
// @bycapwan