Retrieving the data from a observable

Hello, I am currently learning Typescript/Angular, so please excuse me if this question has already been asked (I couldn't find it) or if it has a simple solution :)

My goal is to have a timer displayed on my webpage. I have implemented it in my component as follows:

this.secondsRemaining$ = timer(0, 1000).pipe(
      map(n => 30 - n),
      takeWhile(n => n >= 0 && someCondition))
);

The timer is displayed on my page using the following code:

{{ secondsRemaining$ | async }} 

Once someCondition becomes false, I want the timer to stop. How can I capture the value at which the timer stopped? Is this achievable with Observables, or should I use a different approach like BehaviorSubject?

Answer №1

To store the value, you can tap and save it to a variable within the local scope, in this case called secondsRemaining:

this.secondsRemaining$ = timer(0, 1000).pipe(
      map(n => 30 - n),
      takeWhile(n => n >= 0 && someCondition),
      tap(n => this.secondsRemaining = n)
  )
);

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

Images are failing to render on Next.js

Hello there! I am facing an issue while working on my Next.js + TypeScript application. I need to ensure that all the images in the array passed through props are displayed. My initial approach was to pass the path and retrieve the image directly from the ...

Angular 7 is taking an unusually long time to load the after login component

Upon logging into my Angular 7 application, the after-login component experiences slow loading for the first time. However, if a user logs out and then logs back in without closing the browser, the speed improves. The routing structure is as follows: In ...

An error occurred while running React, Next.js, and Type Script: Unhandled Runtime Error - TypeError: Unable to access 'value' property of undefined

I have been working on a multi-page form and using the antd package to add some styling to it. On the main page of the form, I implemented the following code (making sure I imported everything required). export class CreateNewContract extends Component ...

Progress Bar Modules

I am currently working on creating a customizable animated progress bar that can be utilized as follows: <bar [type]="'health'" [percentage]="'80'"></bar> It is functional up to the point where I need to adjust different p ...

The nz-switch function is malfunctioning as a result of an update that has affected its value

<form [formGroup]="businessFoodHygieneForm"> <div class="box p-4 d-flex jc-between ai-center"> <span> Food Hygiene Link </span> <label> <nz-switch class="switch- ...

Utilize Angular6 and NodeJS to exhibit images stored on a server file system

Successfully uploaded images to a server but struggling to find a tutorial on how to display these images using Angular6 and NodeJS. Any help would be greatly appreciated. Thank you in advance. Edit: After many trials and errors, I was able to retrieve a ...

Generating an array of strings that is populated within the Promise handler

If I come across code like this in my Ionic/Angular/TypeScript project... let arr: Array<string> = []; this.databaseProvider.getAllSpecies().then(allSpecies => { for(let species of allSpecies) { if(species.name.toLowerCase().indexOf(keyword ...

Missing from the TypeScript compilation are Angular5's polyfills.ts and main.ts files

Here is the structure of my Angular5 project. https://i.stack.imgur.com/tmbE7.png Within both tsconfig.app.json and package.json, there is a common section: "include": [ "/src/main.ts", "/src/polyfills.ts" ] Despite trying various solu ...

DomSanitizer in Angular is not able to bind to an Angular component

Trying to dynamically insert HTML content into a DIV has been successful when done statically. The following code is functioning properly: <popover-content #pop1 title="Cool Popover" placement="right" ...

Converting a nested JSON object into a specific structure using TypeScript

In the process of developing a React app with TypeScript, I encountered a challenging task involving formatting a complex nested JSON object into a specific structure. const data = { "aaa": { "aa": { "xxx&qu ...

Guide on invoking an API with a private IP on an EC2 instance

I successfully deployed an Angular app and a Java REST-API on my ec2-instance. When accessing the Java REST-API via the public IP in my Angular app, everything works fine. However, if I try to use the private IP of my instance, it results in a connection ...

Tips for type guarding in TypeScript when using instanceof, which only works with classes

Looking for a way to type guard with TypeScript types without using instanceof: type Letter = 'A' | 'B'; const isLetter = (c: any): c is Letter => c instanceof Letter; // Error: 'Letter' only refers to a type, but is being ...

"Utilizing the same generic in two interface properties in Typescript necessitates the use of the

I have created an interface as follows: interface I<T>{ foo: T arr: T[] } After defining the interface, I have implemented an identity function using it: const fn = <T>({foo, arr}: I<T>) => ({foo, arr}) When calling this function l ...

What is preventing my Angular form from displaying the input data correctly?

I've been diligently following a PDF tutorial step by step, but for some reason, I'm not achieving the same results. Despite thorough googling, I can't seem to figure out where I'm going wrong. Here's the content of student.compon ...

Tips for effectively utilizing Mongoose models within Next.js

Currently, I am in the process of developing a Next.js application using TypeScript and MongoDB/Mongoose. Lately, I encountered an issue related to Mongoose models where they were attempting to overwrite the Model every time it was utilized. Here is the c ...

Sharing data among components in Angular 6

I've set up 2 components and a service as outlined below: component-interaction.service.ts @Injectable() export class ComponentInteractionService { public dataSubject = new BehaviorSubject<string>("Test"); getTestData(): Observable<an ...

Having trouble setting up tailwind css for my Angular project, need some help

Initially, I was referencing this article (along with others): https://medium.com/@jacobneterer/angular-and-tailwindcss-2388fb6e0bab I've been attempting to integrate Tailwind into my Angular project without success. Additionally, I even started an A ...

Customize the index of NgFor in Angular to have more control over how items are

Currently, I am working with this ngIf block: <div class="clearfix" *ngFor="let item of result.items;let i = index"> <h5 *ngIf="itHasOpt(item)"> <span class="numberCircle">{{ i+1 }}</span> </h5> ... As the i valu ...

"Upon compilation, the Angular app displays a blank screen instead of the expected

Recently, I attempted to create a client for a web application using Angular. I initiated the process with ng new client, and when I ran it at that point, it displayed the default webpage. Afterwards, I made modifications to the app.component.{html, css ...

Having trouble verifying a user's password in Postman using "bcrypt.compareSync" with Angular and the MEAN stack

I'm currently working on implementing user authentication before adding a JWT token, and I've encountered an issue where the 'if' check is generating this error: node_modules\bcryptjs\dist\bcrypt.js:265 th ...