Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
How to edit this function to make 3 things instead of 2

I use this function with cache to make if the user enter the site for first time in last x minutes redirect him and if this is the second time press the button i now want to add if this is the third time in last x minutes to do another thing.

function getCookie(name) {
  const value = `; ${document.cookie}`;
  const parts = value.split(`; ${name}=`);
  if (parts.length === 2) return parts.pop().split(";").shift();
}

function setLastAccess() {
  const date = new Date();
  const expireMs = 0.5 * 60 * 1000; // number of minutes
  date.setTime(date.getTime() + expireMs);
  document.cookie = `lastAccess=${new Date().getTime()};expires=${date.toUTCString()};path=/`;
}

if (!getCookie('lastAccess')) {
  window.location.href = "http://google.com";
  setLastAccess(); // set your last access at the end
} else {
  setTimeout(function() {
    document.getElementById('button-login').click();
  }, 1000);
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can maintain a counter in localStorage whenever user access the site, and update it. Check for the count and if it is 3, run your logic.

You can also store it in cookie if it needs to be transferred to server.

function getCookie(name) {
  const value = `; ${document.cookie}`;
  const parts = value.split(`; ${name}=`);
  if (parts.length === 2) return parts.pop().split(";").shift();
}

function setLastAccess() {
  const date = new Date();
  const expireMs = 0.5 * 60 * 1000; // number of minutes
  date.setTime(date.getTime() + expireMs);
  document.cookie = `lastAccess=${new Date().getTime()};expires=${date.toUTCString()};path=/`;
}

function numAccess() {
  if(!localStorage.getItem('accessCount')) {
    localStorage.setItem('accessCount', '1');
  } else {
    localStorage.setItem(+localStorage.getItem('accessCount') + 1)
  }
}

if (!getCookie('lastAccess')) {
  window.location.href = "http://google.com";
  setLastAccess(); // set your last access at the end
  numAccess();
} else {
  setTimeout(function() {
    numAccess();
    document.getElementById('button-login').click();
  }, 1000);
}

if(+localStorage.getItem('accessCount') === 3) {
  // Do some stuff
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda