-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti-temporizador.html
179 lines (167 loc) · 5.07 KB
/
multi-temporizador.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
<!DOCTYPE html>
<html lang="es-AR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multi-temporizador</title>
<style>
html,body{
height: 100%;}
body{
display: grid;grid-template-rows: 1fr auto;gap:5px;
margin: 0;padding: 5px;box-sizing: border-box;
font-family: sans-serif;}
#timers{
display: flex;flex-direction: column;gap: 5px;
overflow: auto;}
.timer{
display: grid;grid-template-columns: 1fr repeat(5,auto);grid-template-areas: "a a a a a a" "b c d e f g";
border: solid 1px;padding: 5px;}
.tag{
grid-area: a;text-align: center;padding: 5px 5px 10px 5px;}
.display{
text-align: center;height: min-content;margin: auto;}
.blink{
animation: blink 1s infinite;}
.timer > select{
height: calc(100% - 2px);margin: 1px;}
body > button{
padding: 10px 0;}
#choser{
display: none;
position: fixed;height: 100%;width: 100%;top: 0;left: 0;
background: rgba(125,125,125,.5);}
#choser > div{
background: white;padding: 10px;border-radius: 10px;
display: grid;margin: auto;}
@keyframes blink {
0%{color:black;}
50%{color:red;}
}
</style>
<script>
var timers=[]
,sonidoSrc='https://onlineclock.net/audio/options/default.mp3';
class Temporizador{
constructor(h,m,s,tag){
this.audio=new Audio(sonidoSrc);
this.audio.loop=true;
this.ticks=Math.floor(h*60*6 + m*6 + s);
this.id=timers.push(this);
let timersHolder=gEt('timers')
,template=document.createElement('template');
template.innerHTML=`
<div data-id=${this.id} class=timer>
<span class=tag>${tag}</span>
<span class=display>${formatTime(h,m,s)}</span>
<button value="pause/play">⏸</button>
<select>
<option value=0>s</option>
<option value=1>m</option>
<option value=2>H</option>
</select>
<button value=mas>➕</button>
<button value=menos>➖</button>
<button value=eliminar>❌</button>
</div>`.replaceAll(/[\n\t]/g,'');
timersHolder.append(template.content.firstElementChild);
this.cosas=timersHolder.lastElementChild.children;
this.timeHolder=this.cosas[1];
this.state=1;
this.start();
}
change(type){
let cuanto=60**this.cosas[3].value;
switch(type){
case 'pause/play':
this.state=this.state==1?2:1;
if(this.state==1)
this.start();
this.cosas[2].innerText=this.state==1?'⏸':'▶';
break;
case 'mas':
this.ticks+=cuanto;
if(!this.audio.paused){
this.audio.pause();
this.cosas[2].disabled=false;
this.timeHolder.classList.remove('blink');
this.start();
}
this.updateDisplay();
break;
case 'menos':
if(this.ticks>=cuanto){
this.ticks-=cuanto;
this.updateDisplay();
break;
}/* else eliminar */
case 'eliminar':
this.state=0;
if(!this.audio.paused)
this.audio.pause();
this.cosas[0].parentNode.remove();
break;
}
}
updateDisplay(){
this.timeHolder.innerText=formatTime(Math.floor(this.ticks/(60 * 60)),Math.floor(this.ticks/60%60),this.ticks%60);
}
start(){
this.intervalID=setInterval(()=>{
if(this.state==0 || this.state==2){
clearInterval(this.intervalID);
return;
}
this.ticks--;
if(this.ticks==0){
clearInterval(this.intervalID);
this.cosas[2].disabled=true;
this.timeHolder.classList.add('blink');
this.audio.play();
}
this.updateDisplay();
},1000);
}
}
function gEt(sel){
return document.getElementById(sel);
}
function añadir(){
let horas=gEt('horas').value
,minutos=gEt('minutos').value
,segundos=gEt('segundos').value
,etiqueta=gEt('etiqueta').value.trim();
if(etiqueta!='' && (horas!=0 || (minutos!=-1 && minutos!=0) || (segundos!=-1 && segundos!=0)))
new Temporizador(horas,minutos,segundos,etiqueta);
gEt('choser').click();
}
function formatTime(h,m,s){
return `${h}:${m<10?0:''}${m}:${s<10?0:''}${s}`;
}
addEventListener('DOMContentLoaded',()=>{
/* UI */
let options=new Array(60).fill(0).reduce((acc,el,i)=>acc+`<option value=${i}>${i}</option>`,'');
gEt('minutos').innerHTML=gEt('segundos').innerHTML=options;
/* handlers */
gEt('timers').onclick=e=>{
let target=e.target;
if(target.nodeName=='BUTTON')
timers[+target.parentNode.dataset.id-1].change(target.value);
}
})
</script>
</head>
<body>
<div id="timers"></div>
<button onclick="this.nextElementSibling.style.display='flex'">Añadir</button>
<div id="choser" onclick="if(event.target==this)this.style.display='none'">
<div>
Etiqueta: <input id="etiqueta">
Horas: <input type=number id="horas" step=1 min="0" value=0></input>
Minutos: <select id="minutos"></select>
Segundos: <select id="segundos"></select>
<button onclick="añadir()">Añadir</button>
</div>
</div>
</body>
</html>