• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

209
Views
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 3 years ago · Juan Pablo Isaza
2 answers
Answer question

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 3 years ago · Juan Pablo Isaza Report

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 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error