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

170
Visualizações
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 Respostas
Responde à pergunta

0

after clearTimeouts call this code

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

about 4 years ago · Juan Pablo Isaza Relatório

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 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