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

195
Visualizações
Running a While function again, only if specific data has been loaded

I'm trying to get a while function to run again only after it has successfully loaded the length of an external audio file.

The goal is to retrieve the length of each audio file that has been listed in an array.

Right now it only retrieves the length of the last audio file.

I'm assuming this is because it takes a while to get the length of an audio file, while the While function has already jumped to the next loop.

Here's the code:

var myList = [
  'https://mixergy.com/wp-content/audio/Nick-Bolton-Animas-on-Mixergy0721.mp3',
  'https://episodes.castos.com/5e7027dcc7b720-84196812/MicroCOnf-on-Air-Refresh.Ep.6.mp3'
]

var timeNow = 100;
var listLength = 0;


// Get audio file
// Create a non-dom allocated Audio element
var au = document.createElement('audio');


var i = 0;

while(i < (myList.length-1)) {
  // Define the URL of the MP3 audio file
  au.src = myList[i];
    console.log(myList[i])
    console.log(i)
    listLength = Math.round(au.duration);

  // Once the metadata has been loaded, display the duration in the console
  au.addEventListener('loadedmetadata', function(){
  // Obtain the duration in seconds of the audio file
  listLength = Math.round(au.duration);

    console.log(listLength)

  },false);

  i++;

}
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>


    <script src="https://code.jquery.com/jquery-3.6.0.slim.js" integrity="sha256-HwWONEZrpuoh951cQD1ov2HUK5zA5DwJ1DNUXaM6FsY=" crossorigin="anonymous"></script>
    <script src="script.js" charset="utf-8"></script>
  </body>
</html>

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

0

This is because the function keep reference to the same variable au which changes, so when loadmetadata method is executed it uses the latest element. You should use a function and in this case au variable will become local to that function, something similar to this:

function checkLength(src) {
  var au = document.createElement('audio');
  au.src = src;
  listLength = Math.round(au.duration);

  // Once the metadata has been loaded, display the duration in the console
  au.addEventListener('loadedmetadata', function(){
    // Obtain the duration in seconds of the audio file
    listLength = Math.round(au.duration);

    console.log(listLength)

  },false);

}

var myList = [
  'https://mixergy.com/wp-content/audio/Nick-Bolton-Animas-on-Mixergy0721.mp3',
  'https://episodes.castos.com/5e7027dcc7b720-84196812/MicroCOnf-on-Air-Refresh.Ep.6.mp3'
]

var timeNow = 100;
var listLength = 0;


// Get audio file
// Create a non-dom allocated Audio element
var i = 0;

while(i < (myList.length-1)) {
  // Define the URL of the MP3 audio file
  checkLength(myList[i]);
  i++;
}
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