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

153
Visualizações
Can someone tell me why this JSfiddle repeats the first song when finishes playing?

This is a super quick question. Here is a piece of code that plays an array of songs in order. Somehow, when the array finishes playing, it repeats the first song. Any idea why? I am going insane because I cant figure it out...

Code below in the link: https://jsfiddle.net/wt2joay4/

var song1 = $('#sound-1');
var song2 = $('#sound-2');
var song3 = $('#sound-3');

var audioArray = [song1, song2, song3];
var i=0;
var lastPlayedFile = null;
$(".click").click(function(){
  if(lastPlayedFile !== null) {
     lastPlayedFile[1].currentTime = 0;
     lastPlayedFile.trigger('pause'); 
  }
  if (i< audioArray.length){
     lastPlayedFile = audioArray[i];
     audioArray[i].trigger('play');
     i++;
} else if (i>=audioArray.length){
     i = 0;
     lastPlayedFile = audioArray[0];
     audioArray[i].trigger('play');
  };
});

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The issue is with i=0. This should be i=1.

The i variable is used to indicate which song to play next rather than current index.

So in i<audioArray.length it plays audioArray[i] and then does i++.

But in i>=length it plays audioArray[0] but leaves i pointing at 0, so the next played is again 0.

The fix is to leave i=1 after re-looping. This could use the same concept as above: i=0; ...play [i]...; i++; or just i=1.

} else if (i>=audioArray.length){
     lastPlayedFile = audioArray[0];
     audioArray[i].trigger('play');
     i = 1;
  };
});

Suggestion: Use a better variable name than i. If this was named nextIndex then nextIndex=0 would be clear(er) that it's going to play 0 next (when it's already playing 0).

Alternative suggestion: Keep i as the current index and increase before starting the next, eg:

var song1 = $('#sound-1');
var song2 = $('#sound-2');
var song3 = $('#sound-3');

var audioArray = [song1, song2, song3];
var i = -1;
$(".click").click(function() {

  if (i >= 0) {
    audioArray[i].currentTime = 0;
    audioArray[i].trigger('pause');
  }

  i++;
  if (i >= audioArray.length)
    i = 0;

  audioArray[i].trigger('play');
});

(and change i to currentIndex, but left as i above to compare with original)

updated fiddle

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