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

262
Views
Play songs in sequence in JS

I'm trying to make an MP3 random player. I've already done every array and cycles, but what it struggles me is that every songs plays all in the same time, basically something like that:

var num; var x = 0; let music = ['https://samplelib.com/lib/previeww/mp3'sample-3s.mp3', 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3']; funcion button(){ for (let i = 0; i < 4; i++){ num = Math.floor(Math.random()*3);

console.log(num);} music.play(num)} music[num].addEventListener('ended', console.log("placeholder")});

What happens, as I porevious said before, the cycle doesn't wait the sound to finish, it goes back over and over again until the page crashes. Is there any way to make him wait before it plays again?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You're going to need to provide more context for us to give you a really good answer.

My guess is that whatever function you're calling in the for loop is happening asynchronously. Basically, your for loop may be pushing the start button 4 times in a row without waiting for the end to happen.

If you look at the documentation for your mp3 player object/function that you're calling in the for loop you may be able to identify a function that checks when the song is over and then wait for that before you call the next iteration of the for loop

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!