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

287
Visualizações
local time doesnt save in local storage after page refresh

Im quite a newbie with coding

So i got the code below, the local time is being set correctly on the webpage and in local storage. However, after refreshing the time sets again and i dont need that. I was looking through several topics and couldnt find anything that fits my problem. Tried document.write() etc, but didnt work

What could be the solution to this problem or what are the issues in my code that the time sets new after refresh?

const date = new Date().toLocaleTimeString();
document.getElementById('entryTime').textContent = date


localStorage.setItem('time', JSON.stringify(date));

if (localStorage.getItem("time")) {
  entryTime = JSON.parse(localStorage.getItem("time"));
} else {
  // No data, start with an empty array
  entryTime = [];
}
console.log(date);
document.getElementById('entryTime').innerHTML = date
<div class="row" style="margin-top: 10px !important; margin-bottom: 1px !important; font-size: 30px;" id="entryTime"></div>

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

0

The first thing you do when the page loads is, unconditionally, to get the time and store it in Local Storage.

If you only want to set the time to LS when there isn't a time there already, try to read it first and only set it if it isn't set already.

about 4 years ago · Juan Pablo Isaza Relatório

0

Does this work for you?

var entryTime;
if (localStorage.getItem("time")) {
    entryTime = JSON.parse(localStorage.getItem("time"));
} else {
    entryTime  = new Date().toLocaleTimeString();
    localStorage.setItem('time', JSON.stringify(entryTime ));
}
document.getElementById('entryTime').innerHTML = entryTime;
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