Angular/Typescript - Get timestamp OnDestroy

176 views Asked by At

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);
  }
2

There are 2 answers

0
Arun Mohan On BEST ANSWER

Call this inside ngDestroy and then console

ngOnDestroy(): void {
  this.exitTime = new Date();
  console.log(this.exitTime);
  }
0
Shlok Nangia On

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);
  }