Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
Unsubscribe from a observable when condition is true in angular

I'm trying close a popup after 5 seconds when it is loaded but the subscription finish before so i want to unsubscribe when the variable loaded is true

    this.closed = false;
    this.loaded$.subscribe(loaded => {
      if (loaded) {
        setTimeout(() => {
          this.modalService.dismissActiveModal();
        }, 5000);
      }
    }).unsubscribe();
  } 
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

this.closed = false;
// define the subscription
const subscription: Subscription = this.loaded$.subscribe(loaded => {
  if (loaded) {
    setTimeout(() => {
      this.modalService.dismissActiveModal();
      subscription.unsubscribe(); // unsubscribe here
    }, 5000);
  }
});

How about this?

about 4 years ago · Santiago Trujillo Report

0

this.closed = false;
this.loaded$
.pipe(
  filter(loaded => loaded),
  switchMap(_ => interval(5000)),
  take(1),
  tap(_ => this.modalService.dismissActiveModal())
)
.subscribe();
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!