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

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

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