You can create a method that returns the current date and time:
function DataHora() {
var date = new Date(), hora = date.getHours(), minuto = date.getMinutes(), dia = date.getDate(), mes = (date.getMonth() + 1), ano = date.getFullYear();
if (dia < 10) dia = '0' + dia;
if (mes < 10) mes = '0' + mes;
if (hora < 10) hora = '0' + hora;
if (minuto < 10) minuto = '0' + minuto;
return [ [dia, mes, ano].join('/'), [hora, minuto].join(':') ];
}
the method DataHora
has as return an array, being the date and time:
["24/04/2018", "04:19"]
create a variable to store the current message and the previous one, to be able to compare them later preventing the alert
enter an infinite loop:
var lembrete = '';
var anterior = '';
then you go through the object horaJSON.Aviso
and compares the date and time of each item with the date and time returned by the method. If true, set the variable value, otherwise do nothing.
horaJSON.Aviso.forEach(item => {
if ( item.data === DataHora()[0] && item.hora === DataHora()[1] ) {
lembrete = 'Data: ' + item.data + '\nHora: ' + item.hora + '\nLembrete: ' + item.dica;
}
});
to finish, check if the variable lembrete
is not empty and is different from anterior
, if I was not empty and is different, display the message and set the value of lembrete
in anterior
, otherwise do nothing.
(lembrete !== '' && lembrete !== anterior) && (alert(lembrete) & (anterior = lembrete));
create a method and place the loop and condition if
up in it.
function exibeLembrete() {
horaJSON.Aviso.forEach(item => {
if ( item.data === DataHora()[0] && item.hora === DataHora()[1] ) {
lembrete = 'Data: ' + item.data + '\nHora: ' + item.hora + '\nLembrete: ' + item.dica;
}
});
(lembrete !== '' && lembrete !== anterior) && (alert(lembrete) & (anterior = lembrete));
}
now set a time interval for the method to run:
setInterval(exibeLembrete, 1000);
Complete code
You can see the code working on jsbin.com, just edit the dates and times that are in the object.
var horaJSON = {
"Aviso":[
{
"data":"24/04/2018",
"hora":"06:00",
"dica":"Café no bule."
}, {
"data":"24/04/2018",
"hora":"14:40",
"dica":"Caminhada no parque."
}, {
"data":"24/04/2018",
"hora":"14:41",
"dica":"Preparar o almoço."
},
]
},
lembrete = '',
anterior = '';
function DataHora() {
var date = new Date(), hora = date.getHours(), minuto = date.getMinutes(), dia = date.getDate(), mes = (date.getMonth() + 1), ano = date.getFullYear();
if (dia < 10) dia = '0' + dia;
if (mes < 10) mes = '0' + mes;
if (hora < 10) hora = '0' + hora;
if (minuto < 10) minuto = '0' + minuto;
return [ [dia, mes, ano].join('/'), [hora, minuto].join(':') ];
}
function exibeLembrete() {
horaJSON.Aviso.forEach(item => {
if ( item.data === DataHora()[0] && item.hora === DataHora()[1] ) {
lembrete = 'Data: ' + item.data + '\nHora: ' + item.hora + '\nLembrete: ' + item.dica;
}
});
(lembrete !== '' && lembrete !== anterior) && (alert(lembrete) & (anterior = lembrete));
}
setInterval(exibeLembrete, 1000);
I guess this didn’t turn into the "snippet", but I’m giving a studied line-by-line of your answer.
– Diego Henrique
In order for it to work, there had to be a
loop
infinity - so I made an HTML document named "event.html" and, I entered it between thehead
onemeta
. Namely:<META HTTP-EQUIV="REFRESH" CONTENT="1;URL=evento.html">
. This causes the page when redirecting, calls itself, creating aloop
infinity. Dai funfa that is a beauty.– Diego Henrique
Actually it was a mistake of mine, at the time I was writing, I wrote within a method, at the time of putting here I removed it. But now look how it turned out.
– NoobSaibot