To some form of progressive count to the limit set by Hour, sound alert

Asked

Viewed 83 times

0

What mode can I use Event alert by scheduling some schedules

So prepare to carry out a different reminder every hour of the day by issuing a warning

Suppose we have a JSON in that structure:

var horaJSON = {
    "Aviso":[
    {
        "data":"24/03/2018",
        "hora":"06:00",
        "dica":"Café no bule."
    },
        "data":"24/03/2018",
        "hora":"09:00",
        "dica":"Caminhada no parque."
    },
        "data":"24/03/2018",
        "hora":"11:00",
        "dica":"Preparar o almoço."
    },
    ]
};

Above we have a Object Array "Warning" where we can use it to iterate the contidian events.

    alert(" Hoje:"+horaJSON.Aviso[2].data+"\n Hora: "+horaJSON.Aviso[2].hora+"\n Lembrete: "+horaJSON.Aviso[2].dica);

So the question is - "How would you make this comparison and validation?"

1 answer

2


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.

  • 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 the head one meta. Namely: <META HTTP-EQUIV="REFRESH" CONTENT="1;URL=evento.html">. This causes the page when redirecting, calls itself, creating a loop infinity. Dai funfa that is a beauty.

  • 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.