<div id="countdown">0</div>
<div id="mensaje"></div>
<hr>
<h2>Lista de recordatorios:</h2>
<div id="lista"></div>
<button id="actualizar">Actualizar</button><br><br>
<h2>Agregar recordatorio:</h2>
Mensaje: <input type="text" id="agregarMensaje"><br>
Tiempo: <input type="number" id="agregarTiempo"><br>
<button id="agregar">Agregar</button><br><br>
<h2>Quitar recordatorio:</h2>
No es necesario colocar los 2 datos, con uno es suficiente.<br>
Mensaje: <input type="text" id="quitarMensaje"><br>
Tiempo: <input type="number" id="quitarTiempo"><br>
<button id="quitar">Quitar</button><br><br>
<h2>Eliminar todos los recordatorios:</h2>
<button id="quitarTodo">Limpiar</button><br><br>
<h2>Reiniciar temporizador:</h2>
<button id="reiniciar">Reiniciar</button>
<script>
//SE DECLARA EL OBJETO
class Temporizador { constructor() { if(localStorage.getItem("tiempo") == null){ this.reiniciar();
}
this.extra=1000-1000*((((new Date()-0)-parseInt(localStorage.getItem("tiempo")))/1000)-this.getTime()); }
reiniciar(){ localStorage.setItem("tiempo", (new Date()-0)); }
getTime(){ return parseInt(((new Date()-0)-parseInt(localStorage.getItem("tiempo")))/1000); }
crearRecordatorio(msg, s){ this.x = localStorage.getItem("recordatorio"); if(this.x == null || this.x == ""){ localStorage.setItem("recordatorio", (s + ";" + msg)); } else { localStorage.setItem("recordatorio", (this.x + ";" + s + ";" + msg)); }
}
eliminarRecordatorio(inp){ this.x3 = this.recordatorio();
this.res="";
this.r=false;
if(Number.isInteger(inp)){ for(var i = 0; i < this.x3.length; i+=2){ if(this.x3[i] != inp){ if(this.r){ this.res+=";";
} else { this.r=true;
}
this.res+=this.x3[i]+";"+this.x3[i+1];
}
}
} else { for(var i = 1; i < this.x3.length; i+=2){ if(this.x3[i] != inp){ if(this.r){ this.res+=";";
} else { this.r=true;
}
this.res+=this.x3[i-1]+";"+this.x3[i];
}
}
}
localStorage.setItem("recordatorio", this.res); }
limpiarRecordatorio(){ localStorage.removeItem("recordatorio"); }
recordatorio(){ this.x4 = localStorage.getItem("recordatorio"); if(this.x4 != null){ return this.x4.split(";"); } else { return false;
}
}
reordenarRecordatorio(){ this.x2=this.recordatorio();
this.l=this.x2.length;
this.ram=Array();
for(var i=this.l/2-1; i>0; i--){ for(var i2=0; i2<(i*2+1); i2+=2){ if(parseInt(this.x2[i2]) > parseInt(this.x2[i2+2])){ this.ram[0]=this.x2[i2];
this.ram[1]=this.x2[i2+1];
this.x2[i2]=this.x2[i2+2];
this.x2[i2+1]=this.x2[i2+3];
this.x2[i2+2]=this.ram[0];
this.x2[i2+3]=this.ram[1];
console.log(this.x2[i2]+";"+this.x2[i2+1]+";"+this.x2[i2+2]+";"+this.x2[i2+3]);
}
}
}
this.limpiarRecordatorio();
for(var i=0; i<this.l; i+=2){ this.crearRecordatorio(this.x2[i+1], this.x2[i]);
}
}
init() { document.getElementById('countdown').innerHTML = this.getTime();
setTimeout(() => { setInterval(() => { document.getElementById('countdown').innerHTML = this.getTime(); this.x5=this.recordatorio();
this.ok=false;
if(this.x5){ for(var i = 0; i < this.x5.length; i+=2){ if(this.getTime() >= this.x5[i]){ document.getElementById('mensaje').innerHTML = this.x5[i+1]; this.ok=true;
}
}
}
if(!this.ok){ document.getElementById('mensaje').innerHTML = "Nada"; }
},1000);
}, this.extra);
}
}
//SE PREPARA
//SE CREA EL OBJETO DENTRO DE UNA VARIABLE PARA SER USADO
var temporizador = new Temporizador();
//SE INICIALIZA EL TEMPORIZADOR
temporizador.init();
//FUNCIONES
//FUNCION QUE RESTABLECE EL CRONOMETRO
function restablecer(){ temporizador.reiniciar();
}
//FUNCION QUE ACTUALIZA LA LISTA
function actLista(){ var lista = temporizador.recordatorio();
var res="";
if(lista){ for(var i=0; i<lista.length; i+=2){ res+=lista[i+1]+": "+lista[i]+"<br>";
}
} else { res = "No se encontraron recordatorios.";
}
document.getElementById("lista").innerHTML = res; }
//FUNCION PARA AGREGAR RECORDATORIO
function agregarRecordatorio(){ var mensaje = document.getElementById("agregarMensaje").value; var tiempo = document.getElementById("agregarTiempo").value;
if(mensaje && tiempo){ temporizador.crearRecordatorio(mensaje, tiempo);
document.getElementById("agregarMensaje").value=""; document.getElementById("agregarTiempo").value="";
temporizador.reordenarRecordatorio();
actLista();
} else { alert("Introduce el mensaje y el tiempo que quieres agregar a la lista de recordatorios."); }
}
//FUNCION PARA QUITAR RECORDATORIO
function quitarRecordatorio(){ var mensaje = document.getElementById("quitarMensaje").value; var tiempo = parseInt(document.getElementById("quitarTiempo").value); var error = true;
if(tiempo){ temporizador.eliminarRecordatorio(tiempo);
error=false;
} else if(mensaje){ temporizador.eliminarRecordatorio(mensaje);
error=false;
} else { alert("Introduce el mensaje o el tiempo que quieres eliminar de la lista de recordatorios."); }
if(!error){ document.getElementById("quitarMensaje").value=""; document.getElementById("quitarTiempo").value=""; temporizador.reordenarRecordatorio();
actLista();
}
}
//FUNCION PARA LIMPIAR LA LISTA
function quitarTodo(){ temporizador.limpiarRecordatorio();
actLista();
}
//FUNCIONAMIENTO
//PREPARACION
actLista();
//DETECCION DE PULSACION EN BOTONES
document.getElementById("actualizar").onclick=actLista; document.getElementById("agregar").onclick=agregarRecordatorio; document.getElementById("quitar").onclick=quitarRecordatorio; document.getElementById("quitarTodo").onclick=quitarTodo; document.getElementById("reiniciar").onclick=restablecer;
</script>