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

162
Views
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 answers
Answer question

0

after clearTimeouts call this code

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

about 4 years ago · Juan Pablo Isaza Report

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