Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

251
Visualizações
rxjs countdown that triggers method

I am trying to make a 5 mins countdown that triggers a service method and reloads the 5 mins countdown (basically I am trying to reload some data every 5 mins)

  constructor(
    private reloadDataService: ReloadDataService,
    private userService: UserService
    ) {
      this.timer$ = timer(0, 1000).pipe(
        scan(acc => --acc, 300),
        takeWhile(x => x >= 0),
      );
    }

realod method is trigger by a button or every 5 mins

  reload() {
    this.lastRefresh = new Date();
    this.reloadDataService.reload()
  }

  ngOnInit() {
    this.getUserDetails();
  }

I have tried with

this.timer$ = timer(0, 1000).pipe(
            switchMap(() => this.reload()),
            scan(acc => --acc, 300),
            takeWhile(x => x >= 0),
          );

but didn't work - how can I trigger the reload method every 5 mins?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

use the subscribe property of timer,

const source = timer(0, 300000);
source.subscribe((_) => this.reload());
about 4 years ago · Juan Pablo Isaza Relatório

0

Try below code. It will setup the the interval method and when subscribing to refreshInterval$ you can call reload method or any operation that you want to repeat.

const refreshInterval$: Observable<any> = timer(0, 300000)
  .pipe(
    // Boolean to kill the request if the user closes the component 
    takeWhile(() => this.killTrigger));
refreshInterval$.subscribe(() => {
    // Your code here 
});
about 4 years ago · Juan Pablo Isaza Relatório

0

One way is by using interval or timer function offered by rxjs.

Here is an example that I created.

countDown: Subscription = null;
minutes: number = 5;
counter: number = this.minutes * 60;
triggered: number = 0;

start timer function

startTimer(): void {
  this.countDown ? this.stopTimer() : null;
  this.countDown = interval(1000).subscribe(() => {
    --this.counter;
    if (!this.counter) {
      this.trigger();
    }
  });
}

stop timer function

stopTimer(): void {
  if (this.countDown) {
    this.countDown.unsubscribe();
    this.countDown = null;
  }
}

trigger function that get executed when countdown finish

trigger(): void {
  this.counter = this.minutes * 60;
  this.stopTimer(); // unsubscribe timer
  this.startTimer(); // re subscribe timer
  this.triggered++;
  // your code
}

codesandbox ui example

I hope this helps you, let me know if you build another solution.

Best wishes,

Dev.klodian

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda