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>
>Solution :
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;