0

I'm trying to get a timestamp for every time a page is accessed and another timestamp when the user leaves the page. I get the time Oninit, however 'OnDestroy' I get the same exact time when of when the page was accessed.

entryTime = Date.now();
exitTime = new Date();

ngOnInit(): void {
  console.log(this.entryTime);
  }

ngOnDestroy(): void {
  console.log(this.exitTime);
  }
thelandog
  • 674
  • 1
  • 9
  • 25

2 Answers2

2

Call this inside ngDestroy and then console

ngOnDestroy(): void {
  this.exitTime = new Date();
  console.log(this.exitTime);
  }
Arun Mohan
  • 1,207
  • 1
  • 5
  • 11
0

update the exit time when on destroy is call as it was initialized when the page was loaded

ngOnDestroy(): void {
  this.exitTime=new Date();
  console.log(this.exitTime);
  }
Shlok Nangia
  • 2,355
  • 3
  • 16
  • 24