-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
209 lines (191 loc) · 7.98 KB
/
index.html
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="index.css">
<link rel="apple-touch-icon" sizes="180x180" href="favicon_io/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="./favicon_io/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="./favicon_io/favicon-16x16.png">
<link rel="manifest" href="./favicon_io/site.webmanifest">
<title>Temporizador en Javascript</title>
</head>
<body>
<header class="header">
<h1>Temporizador en Javascript</h1>
</header>
<main class="message">
<h2>Por favor, ingresa un valor para iniciar el temporizador</h2>
</main>
<section>
<div class="container-tiempoRestante">
<div>
<span>Horas</span>
<input type="tel" value="" placeholder="00" id="horas">
</div>
<p style="padding-top: 70px;">:</p>
<div>
<span>Minutos</span>
<input type="tel" value="" placeholder="00" id="minutos" min="0" max="1">
</div>
<p style="padding-top: 70px;">:</p>
<div>
<span>Segundos</span>
<input type="tel" value="" placeholder="00" id="segundos" min="0" max="1">
</div>
</div>
<div class="buttons-section">
<button id="btnPausar" onclick="pausarTemporizador()">Pausar</button>
<button id="btnIniciar" onclick="iniciarTemporizador()">Iniciar</button>
<button id="btnContinuar" onclick="continuarTemporizador()">Continuar</button>
<button id="btnRestablecer" onclick="restablecerTemporizador()">Restablecer</button>
<button id="btnVolverAUsar" onclick="volverAUsar()">Usar de nuevo</button>
</div>
</section>
<script>
const btnPausar = document.getElementById('btnPausar');
const btnIniciar = document.getElementById('btnIniciar');
const btnRestablecer = document.getElementById('btnRestablecer');
const btnContinuar = document.getElementById('btnContinuar');
const btnVolverAUsar = document.getElementById('btnVolverAUsar');
const las_horas = document.getElementById('horas');
const los_minutos = document.getElementById('minutos');
const los_segundos = document.getElementById('segundos');
const message = document.getElementsByClassName('message')[0];
let idInterval = null;
let diferenciaTemporal = 0;
let fechaFuturo = null;
const ocultarElemento = elemento => {
elemento.style.display = 'none';
}
const mostrarElemento = elemento => {
elemento.style.display = '';
}
ocultarElemento(btnPausar);
ocultarElemento(btnIniciar);
ocultarElemento(btnRestablecer);
ocultarElemento(btnContinuar);
ocultarElemento(btnVolverAUsar);
los_minutos.addEventListener('keyup', () => {
if (los_minutos.value > 59) {
los_minutos.value = 59;
}
});
los_segundos.addEventListener('keyup', () => {
if (los_segundos.value > 59) {
los_segundos.value = 59;
}
});
las_horas.addEventListener('change', () => {
mostrarElemento(btnIniciar);
});
los_minutos.addEventListener('change', () => {
mostrarElemento(btnIniciar);
});
los_segundos.addEventListener('change', () => {
mostrarElemento(btnIniciar);
});
if(las_horas.value == '0' || los_minutos.value == '0' || los_segundos.value == '0') {
mostrarElemento(btnRestablecer);
}
const milisegundosaHorasMinutosSegundos = milisegundos => {
const horas = Math.floor(milisegundos / 3600000);
const minutos = Math.floor((milisegundos - (horas * 3600000)) / 60000);
const segundos = Math.floor((milisegundos - (horas * 3600000) - (minutos * 60000)) / 1000);
return {
horas,
minutos,
segundos
};
}
function iniciarTemporizador(){
let horas = document.getElementById('horas');
let minutos = document.getElementById('minutos');
let segundos = document.getElementById('segundos');
if(horas.value == ''){
horas.value = '00';
}else{
horas.value = parseInt(horas.value);
}
if(minutos.value == ''){
minutos.value = '00';
}else{
minutos.value = parseInt(minutos.value);
}
if(segundos.value == ''){
segundos.value = '00';
}else{
segundos.value = parseInt(segundos.value);
}
comenzar_temporizador(horas.value, minutos.value, segundos.value);
}
const restablecerTemporizador = () => {
clearInterval(idInterval);
fechaFuturo = null;
diferenciaTemporal = 0;
las_horas.value = "";
los_minutos.value = "";
los_segundos.value = "";
mostrarElemento(message);
ocultarElemento(btnIniciar);
ocultarElemento(btnRestablecer);
ocultarElemento(btnPausar);
ocultarElemento(btnContinuar);
ocultarElemento(btnVolverAUsar);
}
const pausarTemporizador = () => {
clearInterval(idInterval);
ocultarElemento(btnPausar);
ocultarElemento(btnIniciar);
ocultarElemento(btnRestablecer);
mostrarElemento(btnContinuar);
mostrarElemento(btnVolverAUsar);
}
const continuarTemporizador = () =>{
let horas = document.getElementById('horas');
let minutos = document.getElementById('minutos');
let segundos = document.getElementById('segundos');
comenzar_temporizador(horas.value, minutos.value, segundos.value);
mostrarElemento(btnPausar);
mostrarElemento(btnIniciar);
mostrarElemento(btnRestablecer);
ocultarElemento(btnContinuar);
ocultarElemento(btnVolverAUsar);
}
const comenzar_temporizador = (horas, minutos, segundos) => {
ocultarElemento(message);
mostrarElemento(btnPausar);
mostrarElemento(btnRestablecer);
mostrarElemento(btnIniciar);
ocultarElemento(btnContinuar);
ocultarElemento(btnVolverAUsar);
const tiempoEnMiliSegundos = (horas * 60 * 60 * 1000) + (minutos * 60 * 1000) + (segundos * 1000);
fechaFuturo = new Date((new Date().getTime()) + tiempoEnMiliSegundos);
clearInterval(idInterval);
idInterval = setInterval(() => {
const tiempoRestante = fechaFuturo.getTime() - new Date().getTime();
if(tiempoRestante <= 0){
clearInterval(idInterval);
const audio = new Audio('/sonido/alarm.wav');
audio.play();
ocultarElemento(btnPausar);
ocultarElemento(btnRestablecer);
ocultarElemento(btnIniciar);
ocultarElemento(btnContinuar);
mostrarElemento(btnVolverAUsar);
}else{
ocultarElemento(btnIniciar);
let mostrarTiempo = milisegundosaHorasMinutosSegundos(tiempoRestante);
las_horas.value = mostrarTiempo.horas;
los_minutos.value = mostrarTiempo.minutos;
los_segundos.value = mostrarTiempo.segundos;
}
}, 50);
};
const volverAUsar = () => {
location.reload();
}
</script>
</body>
</html>