Using Angular and Typescript to retrieve timestamp during the OnDestroy lifecycle event

I am currently working on capturing timestamps every time a page is loaded and when the user exits the page. I am able to capture the timestamp on OnInit, but having an issue with OnDestroy as it shows the same timestamp as when the page was accessed.

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

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

ngOnDestroy(): void {
  console.log(this.exitTime);
}

Answer №1

Include this code within ngDestroy to print to the console

ngOnDestroy(): void {
  this.endTime = new Date();
  console.log(this.endTime);
}

Answer №2

Change the exit time when the ngOnDestroy method is triggered, as it is currently set to the time of page load.

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The ng test option is failing to execute effectively

Attempting to conduct unit tests utilizing Karma and Jasmine through the ng test is proving to be a bit challenging. Upon issuing the following command: ng test --watch=false --code-coverage --main ./src/main/resources/public/scripts/xyz/workspace/commons ...

Using formArray to dynamically populate data in Angular

I am attempting to showcase the values from a formarray that were previously added. component.ts this.companyForm = this.fb.group({ id: [], company_details: this.fb.group({ ---- }), company_ip_addresses: this.fb.array([this.fb.group({ ...

Angular form grouping radio buttons in a unique way

Encountering some unusual behavior with radio buttons and forms currently. In my form, I have 4 radio buttons set up. The issue I'm facing is that when I click on a radio button, it remains enabled permanently. This means I can have all 4 options sel ...

Is there a way to define one type parameter directly and another type parameter implicitly?

I am currently utilizing a UI-library that offers an API for constructing tables with a structure similar to this: type Column<Record> = { keys: string | Array<string>; render: (prop: any, record: Record) => React.ReactNode; } The l ...

Definition of Jasmine custom matcher type

I have been working on adding typescript definitions to a custom jasmine matcher library. Initially, I successfully added matchers for the generic type T. Now, my goal is to specifically add matchers for DOM elements. While exploring the jasmine type def ...

`Managing a large set of options with Angular Material Select``

Currently, I am utilizing the Angular Material select form control to populate a list of options. However, I have encountered an issue where the list could potentially contain up to 100k elements. Although storing this large list is not problematic, displa ...

Can AngularJs $q promises work alongside async/await syntax?

I am currently working on a unique Angular and AngularJs hybrid application with TypeScript versions 9.07, 1.5.11, and 3.7.5. Our HTTP requests are all handled by a wrapper service written in plain Javascript, which was originally designed for the "legacy" ...

Validate object containing both static and dynamic keys

I'm attempting to create a Yup validation schema for an object with the following structure: interface myObject { prop0: Date prop1: { nestedProp1: string nestedProp2: number [key: string]: string | number } } This is what I have tr ...

Extract data from Angular2 class

I am having trouble with the data binding of {{value}}. Here is a straightforward example: app.component.ts import {Component, OnInit} from "@angular/core"; @Component({ selector: "app", templateUrl: "./app/app.html" }) ...

Differences between Typescript Import and JavaScript import

/module/c.js, attempting to export name and age. export const name = 'string1'; export const age = 43; In b.ts, I'm trying to import the variables name and age from this .ts file import { name, age } from "./module/c"; console.log(name, ...

The possibility exists that the onClick function may be null

I am encountering an issue with a props function that is showing an error message stating that the object may be null. import {Dropdown} from "react-bootstrap"; interface GenreButtonProps { key: number; id: number | null; genre: strin ...

In the context of Expo and TypeScript, the error message "Type '{}' does not have property 'origin'" appears

When attempting push notifications in expo react native, I encountered an error. I am using TypeScript. This is the code I used: The error message indicates that "Property 'origin' does not exist on type '{}'.", and the same for ' ...

Effortlessly adding custom classes in Angular 2 with a breeze

Imagine having a growing Angular2 typescript solution with numerous small classes and objects. Following the best practice, each class is placed in its own file. However, manipulating these objects leads to long lists of imports like: import {Object1} f ...

Contrast between SimpleChange and SimpleChanges

I'm confused about the two terms in Angular 5, SimpleChange and SimpleChanges. Can someone please help explain them to me more clearly than what's in the official documentation? ...

Issue with Angular-CLI freezing during the build process

After transitioning to angular-cli, I encountered an issue with production deployment. While everything runs smoothly on my local Windows 10 PC, the building process freezes at 10% when attempting to build on a CentOS server. Operating System: CentOS 6.6 ...

finding the adjacent li element using the document.(property)

Utilizing a pub/sub solution named ably.io for real-time data updates, I have implemented a method that assigns dynamic ids to each ngFor listing. This allows me to easily identify and update the values received from ably.io subscribe. document.getElement ...

Send a collection of Angular components to an HTML template by passing them as an

Scenario :- I am working on a small Angular application with only two tabs displayed on a single screen, deployed independently as static assets in an AWS S3 bucket. Situation :- Within one of my Angular (version 12) container components, I have lengthy m ...

Issue with RouterLink functionality in Angular 6

While following a Brad Traversy tutorial on coding, I implemented the instructions exactly as given. Below is my 'app.module.ts' file. import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/c ...

Setting up Angular/Fire for Angular 17 deployment

I am currently working on integrating Angular/Fire, the Angular Firebase version, into my Angular 17 application. However, I encountered a dependency error when trying to install it using npm install @angular/fire: npm ERR! Could not resolve dependency: n ...

Top Choice UI Component for Angular 2 Development

As a newcomer to Angular2, I am in the process of building an application from scratch. This application will require extensive use of controls such as Scheduler, Grids, Charts, and others. After researching on Google, I have come across Syncfusion, Kend ...