-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
52 lines (46 loc) · 1.23 KB
/
script.js
File metadata and controls
52 lines (46 loc) · 1.23 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
let alarm = new Audio('alarm.mp3');
let start = false;
let button = document.getElementById('button');
let timer = document.getElementById('timer');
let interval;
function starte() {
if (start == false) {
start = true;
button.src = 'images/stop.png';
startTimer();
} else {
start = false;
button.src = 'images/start.png';
stopTimer();
}
}
function startTimer() {
let startTime = new Date().getTime();
let fiveMinutes = 1000 * 60 * 5;
let endTime = startTime + fiveMinutes;
let updateTimer = function() {
let currentTime = new Date().getTime();
let timeLeft = endTime - currentTime;
if (timeLeft > 0) {
let minutes = Math.floor(timeLeft / (1000 * 60));
let seconds = Math.floor((timeLeft / 1000) % 60);
let text = ('0' + minutes).slice(-2) + ' : ' + ('0' + seconds).slice(-2);
timer.innerHTML = text;
} else {
if (start === true) {
alarm.play();
timer.innerHTML = '05 : 00';
start = false;
button.src = 'images/start.png';
clearInterval(interval);
}
}
};
updateTimer();
interval = setInterval(updateTimer, 500);
}
function stopTimer(){
start = false;
clearInterval(interval);
timer.innerHTML = '05 : 00';
}