Code operating at a given time

Asked

Viewed 585 times

3

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


I tried to study this one that already contains this function, but I didn’t 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();
    }
}
    });
    });

1 answer

4


With Javascript ? It’s simple:

I took only the hours and checked if it fits the quoted time, inside the if is just put the function you want to run.

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 perform 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

  • Gave the following error: Uncaught Syntaxerror: Missing ) after argument list / code I am using: http://pastebin.com/sD7Y49Ef

  • 1

    This correct, just as the AP commented he would like in Rasília’s time, and the function getHours() works with local time. That is there may be discrepancy, for this should be used getUTCHours() which returns the universal time, however for this the AP will have to take into account this difference in its time as well.

  • @Maradinhoph the problem is in your console.log you are passing string without quotes and not concatenating

  • oops! I didn’t notice that detail. It’s okay! Thanks.

Browser other questions tagged

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