-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer.js
100 lines (84 loc) · 2.28 KB
/
timer.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//Dados
var timerElement = window.document.getElementById('timer');
var buttonPomodoro = window.document.getElementById('pomodoro');
buttonPomodoro.addEventListener('click', startPomodoro);
var buttonShortBreak = window.document.getElementById('shortBreak');
buttonShortBreak.addEventListener('click', startShortBreak);
var buttonLongBreak = window.document.getElementById('longBreak');
buttonLongBreak.addEventListener('click', startLongBreak);
var pauseButton = window.document.getElementById('pause');
pauseButton.addEventListener('click', pausePlayFunction)
var resetButton = window.document.getElementById('reset');
resetButton.addEventListener('click', resetFunction)
var chosenTime = 0;
var time = 0;
var minute = 0;
var second = 0;
var interval
var play = true;
var isTimeStarted = false;
//on click dos botões: 25, 5 ou 10 min
function startPomodoro() {
stopClock();
chosenTime = 25;
time = chosenTime * 60;
isTimeStarted = true;
//runTime();
startClock();
}
function startShortBreak() {
stopClock();
chosenTime = 5;
time = chosenTime * 60;
isTimeStarted = true;
startClock();
}
function startLongBreak() {
stopClock();
chosenTime = 10;
time = chosenTime * 60;
isTimeStarted = true;
//runTime();
startClock();
}
//Rodar tempo
function runTime() {
time = time - 1;
timerElement.innerHTML = `${addZero(Math.floor(time / 60))}:${addZero(time % 60)}`;
console.log(time)
if (time === 0) {
stopClock();
}
}
function addZero(n) {
return n < 10 ? `0${n}` : n;
}
//atualizar a cada segundo
function startClock() {
interval = setInterval(runTime, 1000);
}
//Parar de rodar
function stopClock() {
clearInterval(interval);
}
//Pausar timer
function pausePlayFunction() {
if (play == true && isTimeStarted == true) {
pauseButton.innerHTML = '<i class="fa-solid fa-play"></i>'
stopClock();
play = false;
} else {
if (isTimeStarted == true) {
pauseButton.innerHTML = '<i class="fa-solid fa-pause"></i>'
startClock();
play = true;
}
}
}
//zerar timer
function resetFunction() {
stopClock();
isTimeStarted = false;
time = 0;
timerElement.innerHTML = `${addZero(Math.floor(time / 60))}:${addZero(time % 60)}`;
}