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

120
Visualizações
Javascript Tab on Focus

I currently have the following script that works in the most part however there is a slight modification I would like to make but cannot work how to; I guess with a loop?

Code:

<script>
    var message = "Come Back";
    var original = document.title;
    window.onblur = function() {
        document.title = message;
    }
    window.onfocus = function() {
        document.title = original;
    }
</script>

It currently changes the name of a tab to "Come Back" when focus is lost and then reverts to the original message when the tab is selected again.

I am hoping to add a 2nd "non-focussed" message which the script will switch between every few seconds whilst the tab is not in focus - such as "Come Back", wait 1 second, "Don't Forget".

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First, it's better to use window.addEventListener("blur", function() {}) to wait for the blur event, then set a callback function that will run once the window or the tab has lost focus.

For the callback function you should consider using the setTimeout global method that sets a timer which executes a function or specified piece of code once the timer expires. In your case let's say we want the title to say first "Come back", then after 3 seconds it'll say "Don't forget", and after another 3 secends "Where did you go?", the callback function will look like this:

function() {
  document.title = "Come Back";
  setTimeout(() => {
    document.title = "Don't forget";
  }, 3000); // Runs after 3 seconds of inactivity
  setTimeout(() => {
    document.title = "Where did you go?";
  }, 6000); // Runs after 6 seconds of inactivity
}

Another remark is that the focus event handler you implemented replaces the document.title by original which is a reference to document.title itself and will return the same current title. Instead, the original title should be stored as a hard-coded string and then used to replace the title when the window is focused.

Wrapping everything up, the final code will look like this:

<script>
  const original = "Original title";

  window.addEventListener("focus", function () {
    document.title = original;
  });

  window.addEventListener("blur", function () {
    document.title = "Come Back";
    setTimeout(() => {
      document.title = "Don't forget";
    }, 5000);
    setTimeout(() => {
      document.title = "Where did you go?";
    }, 5000);
  });
</script>

If this answer was helpful, an upvote would be appreciated.

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