How to prevent time change on refresh after displaying once? Trying to achieve the time to be saved once upon generating each html.
const d = new Date();
let text = d.toLocaleTimeString();
document.getElementById("time").innerHTML = text;
<div>
<b>Time Generated: </b><span style="padding-right:410px" id='time'</span>
</div>
You can save the date on the Session storage or Local storage
i choose the first one for this example, but you need to know that Session storage only persists for as long as the current browsing session and is cleared as soon as the browser is closed, if ou want a long term storage you need to use Local Storage.
First check if is the first time on the page:
sessionStorage = window.sessionStorage;
if (!sessionStorage.getItem('date')) {
sessionStorage.setItem('date', new Date().toLocaleTimeString());
}
And then get the date:
text = sessionStorage.getItem('date');
document.getElementById("time").innerHTML = text;
I hope it helps you, have a nice day!