Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
How can I override a JavaScript variable when page is visible again?

When visiting a web page I want to declare a start time and send an AJAX request when the user leaves the site. It works like expected but when changing the tab or minimizing the browser the start time remains the same as when the user accessed the web page. I thought I could override the start time by declaring it within a function which is fired when the tab is active again but with no success.

Here is my code so far:

$(document).ready(function() {
    var starts = Math.ceil(Date.now() / 1000);
    
    //declare new start time when user returns
    document.addEventListener('visibilitychange', function() {
        if(!document.hidden) {
            var starts = Math.ceil(Date.now() / 1000);
        }
    });

    //AJAX
    //value of old starts is used here instead of new declared starts
    ...
});

Does anyone have a solution for this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Try document.visibilityState === 'visible' so your code will look like this:

$(document).ready(function() {
    var starts = Math.ceil(Date.now() / 1000);
    
    //declare new start time when user returns
    document.addEventListener('visibilitychange', function() {
        if(document.visibilityState === 'visible') {
            var starts = Math.ceil(Date.now() / 1000);
        }
    });
});

Read more about it here:

https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilitychange_event

about 4 years ago · Juan Pablo Isaza Denunciar

0

const timer = document.querySelector("#timer");

window.addEventListener('DOMContentLoaded', () => {
  startTimer(); // starttime on load
});
let intervalID = null;
let timerValue = 0;
const startTimer = () => {
  intervalID = setInterval(() => {
    timerValue++;
    timer.innerText = timerValue;
  }, 1000);
}

const stopTimer = () => {
  clearInterval(intervalID);
}

document.addEventListener('visibilitychange', () => {
  if (document.hidden) { // codition for browser tabs is active or minimize
    stopTimer(); // stop timer when you leave tab minimize it on
    return;
  }
  startTimer(); // start timer when you comback to tab maximize it
})
<div class="container">
  <h2>time spend in seconds:<span id="timer">0</span>
    <h2>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda