I have written a component that will keep track of time. I am currently using Localstorage
to save the currentTime
just in case a user hits Refresh. However, I found that this solution is not a really good one becasue the user can edit localstorage
. On the other hand, I can use the database to store this information, but it will slow down my page which I do not want.
Is there any alternative solution beside using LocalStorage
to store my time? I do not want user to edit the localstorage
.
export class MainComponent implements OnInit {
private start: number = null;
private uiTimerId: number = null;
constructor() {
}
private updateUI(): void {
let delta = performance.now() - this.start;
this.someUIElement.textContent = delta.toFixed() + "ms";
}
ngOnInit() {
this.start = parseFloat( window.localStorage.getItem( "timerStart" ) );
if( !this.start ) {
this.start = performance.now();
window.localStorage.setItem( "timerStart", this.start );
}
this.uiTimerId = window.setInterval( this.updateUI.bind(this), 100 ); // 100ms UI updates, not 1000ms to reduce UI jitter
}
buttonClick = function() {
if( this.uiTimerId != null ) {
window.clearInterval( this.uiTimerId );
window.localStorage.removeItem( "timerStart" );
}
}
}