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

167
Vistas
How to clear a sequence of audio with timeouts in React

I have set 4 timeout for audios in my application and I need to stop the audios after user click. The macro function is working correctly, however the clearTimout does not stop the sound. Anyone knows how to clear it?

export function handlePlay(audio) {
  audio.currentTime = 0;
  return audio.play();
}

export function handleConversation(clear) {
  const timer1 = () => setTimeout(() => {
    handlePlay(conversation[Math.floor(Math.random() * conversation.length)]);
  }, TIME1);

  const timer2 = () => setTimeout(() => {
    handlePlay(conversation[Math.floor(Math.random() * conversation.length)]);
  }, TIME2);

  const timer3 = () => setTimeout(() => {
    handlePlay(conversation[Math.floor(Math.random() * conversation.length)]);
  }, TIME3);

  const timer4 = () => setTimeout(() => {
    handlePlay(conversation[Math.floor(Math.random() * conversation.length)]);
  }, TIME4);

  if (clear) {
    console.log('enter clear');
    return () => {
      clearTimeout(timer1);
      clearTimeout(timer2);
      clearTimeout(timer3);
      clearTimeout(timer4);
    };
  }
  timer1();
  timer2();
  timer3();
  timer4();
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

after clearTimeouts call this code

audio.pause();
audio.currentTime = 0;

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here a suggestion of what you could do.

I guess this could be improved further regarding how this handleConversation function is used, I didn't really get the whole idea and there is still some inconsistencies...

 function createAudio(track) {
   track.audio = conversation[Math.floor(Math.random() * conversation.length)];
 }

 export class Track {
   constructor(time) {
     this.time = time;
     this.timeoutid = 0;
     this.audio = new Audio();
   }

   timer() {
     this.timeoutid = setTimeout(() => {
       createAudio(this);
       handlePlay(this.audio);
     }, TIME1);
   }

   play() {
     this.audio.currentTime = 0;
     this.audio.play();
   }

   stop() {
     this.audio.pause();
     this.audio.currentTime = 0;
     clearTimeout(this.timeoutid)
   }
 }

 export function handleConversation(clear) {
   const track1 = new Track(TIME1);
   const track2 = new Track(TIME2);
   const track3 = new Track(TIME3);
   const track4 = new Track(TIME4);

   // this part actually doesn't make a lot of sense, since all tracks will be recreated each time the function is called.
   // I don't really understand how this is used. 
   // I imagine the tracks should more likey be stored outside the function in a persistent object.
   // I will modify my answer if you provide more details about how you use handleConversation
   if (clear) {
     console.log('enter clear');
     return () => {
       [track1, track2, track3, track4].forEach(track => {
         track.stop()
       });
     };
   }

   [track1, track2, track3, track4].forEach(track => {
     track.timer()
   });
 }

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