Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

402
Vistas
How do I fetch an endpoint on interval, stop the interval, and abort the call on leaving page

I am fetching messages every second after the previous call was successful. The problem is, sometimes it takes 2 seconds for the call to finish. During that time, I can navigate back, choose another person and open the conversation with him.

Now, the previous call is successful and populates the area with both conversation data from UserA, and UserB. Furthermore, it continuously fetches the conversations for both UserA, and UserB.

I am suspecting that perhaps I should use clearTimeout(interval) instead of clearTimeout(fetchConversationInterval), or use better code?

But also, how can I abort the still unfinished call if the user navigates back while the messages are being fetched?

I have the following code:

In data()

fetchConversationInterval: null,

On mounted():

this.messagesFetchingInterval()

And on beforeDestroy()

clearTimeout(this.fetchConversationInterval)

In methods:

async messagesFetchingInterval () {
  let that = this
  this.fetchConversationInterval = setTimeout(async function interval () {
    await that.getConversation(that.peerProfile.user_id)
      .finally(()=> {
        if (that.currentRoute === '/direct-messages/conversation') {
          that.fetchConversationInterval = setTimeout(interval, 1000)
        }
      })
  }, 1000)
},
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Async functions (in your case - getConversation) returns their subscription. you can save it in a parameter and then unsubscribe it in on destroy.

var mySubscription;

this.mySubscription = someAsyncFunc().subscribe((result) => {
   console.log(result);
})

ngOnDestroy(){
   this.mySubscription.unsubscribe();
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda