How to schedule a Javascript event?

5

Information: I would like to know how to do Agendamento of an event, ie any function I would pass as a parameter, which would execute such dia tal mês : ano 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 hora when arriving at day minuto at moment "Está na hora!" .

Can you do this in Javascript?

    
asked by anonymous 11.02.2014 / 18:51

1 answer

8

Well, you can do the following, so I understand you want it to be like Scheduler that would be kind of a schedule to run a particular function.

I made it so that you can enter the range too, to be more customized.

In this case, in order not to have to use setInterval() because it will not stop, I've done the best method in performance issues, because it will only execute until I can equal DataAtual with DataNecessaria e so execute your function, and stop.

I used recursion to do this, as you can see I have a função 1 calling a função 2 which calls my initial função 1 again with the same parameters, with a range passed by 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);
}
  

Role Information:

     

schedule , date ], [ time ], [ true ], [ range ])

     

Parameters:

      String - Date by Day / Month / Year (default format), for example "01/01/2014" to execute the function.

     

Time - hh: mm ) - String - Time in Hour: Minute (24h format), for example "23:00" to the function execution.

     

Function - Function ) - Function Send a function reference or function itself for this parameter, will run as soon as you arrive at the date / time you requested.

     

Recursion - (true) - Boolean - Always use true for this parameter, to keep recursion between functions, just use% disable recursion (not recommended).

     

Integer - Interval time to check if the current date equals the given date, in milliseconds ( Beware! Do not put a very large range if you use precision in minutes! )

     

Example

     

If you run:

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

You will result in logs on your browser's console every 5 seconds stating the time required and the current time, when the two match you will have a false 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 did not implement the second because I do not know if you really want to use second-order precision, since the interval would have to be (% recommended) of alert("Está na hora!") to avoid passing the date directly.

I believe that I understood what you requested, 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 with an issue I had with 1s :)

    
11.02.2014 / 18:51