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

268
Visualizações
Javascript Stopwatch. Need help to store my timer and when page refresh to keep the time and continue from there

I have a working stopwatch. When I click start button the stopwatch is starting and when I click pause button the stopwatch is paused. What I would like is when I refresh my browser to keep the current time of the stopwatch and continue from there with the same functionality. Or when I echo the stopwatch time from a database to continue from the exact time it was before is saved it.

let showTime = document.querySelector("#output");
let startTimeButton = document.querySelector("#start")
let pauseTimeButton = document.querySelector("#pause")
pauseTimeButton.style.display = "none";

let seconds = 0;
let interval = null;

const timer = () => {
  seconds++;

  // Get hours
  let hours = Math.floor(seconds / 3600);
  // Get minutes
  let minutes = Math.floor((seconds - hours * 3600) / 60);
  // Get seconds
  let secs = Math.floor(seconds % 60);

  if (hours < 10) {
    hours = `0${hours}`;
  }
  if (minutes < 10) {
    minutes = `0${minutes}`;
  }
  if (secs < 10) {
    secs = `0${secs}`;
  }

  showTime.innerHTML = `${hours}:${minutes}:${secs}`;

};

startTimeButton.addEventListener("click", () => {
  pauseTimeButton.style.display = "block";
  startTimeButton.style.display = "none";
  console.log("START TIME CLICKED");

  if (interval) {
    return;
  }

  interval = setInterval(timer, 1000);
});

// Pause function
pauseTimeButton.addEventListener("click", () => {
  pauseTimeButton.style.display = "none";
  startTimeButton.style.display = "block";
  console.log("PAUSE TIME CLICKED");
  clearInterval(interval);
  interval = null;

});
<button id="start">Start</button>
<button id="pause">Pause</button>
<div id="output"></div>

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

0

You could use localStorage.

In order to save each second passed you could modify your timer function to save to localStorage:


let seconds = 0;
if (localStorage.getItem("stopwatchSeconds") != null) {
    seconds = parseInt(localStorage.getItem("stopwatchSeconds"));
}


//...
const timer = () => {

    //...
    seconds++;
    localStorage.setItem("stopwatchSeconds", seconds);
    //...

};
//...

I hope this helps.

about 4 years ago · Juan Pablo Isaza Relatório

0

It seems like what you're looking for is localStorage. You store your stopwatch data, such as startAt, totalSeconds in the storage and when you refresh the page you can restore your stopwatch state based on the saved data: remainingTime = (startAt + totalSeconds) - currentTime

localStorage docs: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Client-side_storage#storing_simple_data_%E2%80%94_web_storage

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