Code to operate at a certain time

3

I would like to know how I put a code to be active between certain hours, which in this case would be between 7am and 10pm in the Brasilia time zone.


I tried to study this one that already contains this function, do not understand:

          var horaAtual = (new Date()).getHours();
          var imagem = "";
          if(horaAtual >= horario_inicio["manhã"] && horaAtual < horario_inicio

["tarde"]){
              imagem = imagens["manhã"];
          }else if(horaAtual >= horario_inicio["tarde"] && horaAtual < 

horario_inicio["noite"]){
              imagen = imagens["tarde"];
          }else if(horaAtual >= horario_inicio["noite"] || horaAtual < horario_inicio

["manhã"]){
              imagen = imagens["noite"];
          }

          if(imagem_se_repete){
              jQuery(seletor_css).css("background", "url(" + imagem + ") repeat");
          }else{
              jQuery(seletor_css).css({"background-image":"url(" + imagem + ")", 

"background-repeat":"no-repeat", "background-size":"cover"}); 
          }
    });

The code I want to set up is as follows:

$(function() {
$(".post").each(function() {
    if (_userdata.user_level == 2 || _userdata.user_level == 1) {
    return;
    }
if($('.pathname-box:contains(Tarefas), .pathname-box:contains(Redações)').length > 0) {
    var username = _userdata.username;
    if ($(this).has('.staff').length) {
    return;
  }
    if($(this).html().indexOf(username) <= 1) {
          $(this).remove();
    }
    if($(this).html().indexOf(username) >= 1) {
          $(this).remove();
    }
}
    });
    });
    
asked by anonymous 15.10.2015 / 19:07

1 answer

4

With Javascript? It's simple:

I just took the time and checked to see if it fits the given time, inside the if it's just the function that you want to perform.

Example 1:

var d = new Date();
var hora = d.getHours();
if (hora >= 7 && hora <= 22) {
  document.getElementById("demo").innerHTML = hora + ' horario atual';
}
<p id="demo"></p>

If you do not know what time it is to run the function, you can use setInterval() to call your function that validates the schedule and does what you need.

Example 2:

function validaHorario() {
  var d = new Date();
  var hora = d.getHours();
  if (hora >= 7 && hora <= 22) {
    alert(hora + ' horario atual');
  }
}

setInterval(function() {
  validaHorario()
}, 298000); // executa a função validaHorario() de 5 em 5 min
    
15.10.2015 / 19:26