How to schedule an event in Javascript?

Asked

Viewed 1,992 times

6

Information: I would like to know how to accomplish a Agendamento of an event, that is a function that I would pass as a parameter, that would perform such a dia/mês/ano such hora:minuto that I would also pass as a parameter.

Example:

agendar('12/02/2014', '14:55', function() { alert("Está na hora!"); });

An alert would appear saying "Está na hora!" when the day comes 12/02/2014 at the moment 14:55.

You can do this in Javascript?

1 answer

9


Well, you can do the following, from what I understand you want like a Scheduler which would be like a schedule to perform a certain function.

I made it so that you can inform the interval too, to be more customized.

In case, not to have to use setInterval() because it will not stop, I have realized the best method in performance issues, as it will only run until it can match the DataAtual with the DataNecessaria and thus perform its function, and stop.

I used recursion to accomplish such a feat, as you can see I have a função 1 calling a função 2 who calls my função 1 start again with the same parameters, with a range passed per parameter.

See how this function works:

function agendar(data, tempo, func, cond, intervalo) {
  var aryData   = data.split('/'),
      dia       = parseInt(aryData[0]),
      mes       = parseInt(aryData[1]),
      ano       = parseInt(aryData[2]);
  var aryTempo  = tempo.split(':'),
      hora      = parseInt(aryTempo[0]),
      minuto    = parseInt(aryTempo[1]);
      console.log("Necessario Data: "+dia+"/"+mes+"/"+ano+" Tempo: "+hora+":"+minuto);
  var agora     = new Date();
  var diaAtual  = agora.getDate(),
      mesAtual  = (agora.getMonth()+1),
      anoAtual  = agora.getFullYear(),
      horaAtual = agora.getHours(),
      minAtual  = agora.getMinutes();
      console.log("Atual Data: "+diaAtual+"/"+mesAtual+"/"+anoAtual+" Tempo: "+horaAtual+":"+minAtual);
  if (ano == anoAtual && mes == mesAtual && dia == diaAtual && hora == horaAtual && minuto == minAtual) {
    func();
  } else if (cond) {
    cond = false;
    return setTimeout(scheduler, intervalo, data, tempo, func, cond, intervalo);
  }
}
function scheduler(data, tempo, func, cond, intervalo) {
  return setTimeout(agendar, 0, data, tempo, func, true, intervalo);
}

Function Information:

schedule([date],[time],[function],[true],[intermission])

Parameters:

date - (dd/mm/yyyy) String - Date in Day/Month/Year (standard format), for example "01/01/2014" to perform the function.

time - (hh:mm) - String - Time in Hour:Minute(24h format), for example "23:00" for the performance of the.

function - (Function(){}) - Function Send a function reference or function itself to this parameter, it will be executed as soon as it arrives at the date/time you requested.

recursion - (true) - Boolean - Always use true for this parameter, to keep the recursion between functions, just use false if you want to disable recursiveness(not recommended).

intermission - (in milliseconds) - Integer - Interval time to check if the current date is equal to the given date in milliseconds(Beware! Do not put a very large interval if you use minutes-level accuracy!)

Example

If you run:

agendar('11/02/2014', '14:55', function() { alert("Está na hora!"); }, true, 5000);

You will have as a result logs in your browser console every 5 seconds stating the required time and the current time, when the two match you will have a alert("Está na hora!") run on your screen.

Additional Information:

I believe that the recursion parameter would not really be necessary, but I did not succeed in trying to create a function without it.

Also, I didn’t implement the second one, because I don’t know if you really want to use second precision, because the interval would have to be (recommended to be) of 1s to avoid passing the date directly.

I believe I have understood what you asked, as I am subject to change my function if necessary, also, any criticism/opinion will be taken into account.

Credits to @Andreleria for helping me in a problem I had with the setTimeout() :)

Browser other questions tagged

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