Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

286
Vistas
When I click the button, there is no sound effect

I wanted to ask again, based on the answer I got to a question I had asked before. I'm trying to make a sound effect when the buttons are clicked in my project. I saw that the sound effect that I gave direction to the buttons did not materialize. Then I concluded as I posted below. But this doesn't work quite right.

<button href="index.html" type="button" class="menu-icon" onClick="playMyAudio()" preload="auto">Play Audio</button>
<audio src="sound/Message Pop.mp3" preload="auto" id="myAudio"></audio>

<script>
  const audioEl = document.getElementById("myAudio");
  function playMyAudio() {
    audioEl.play();
  }

  const buttonLinks = document.querySelectorAll("button[href]");
  const delay = 75;//milliseconds

  audioEl.addEventListener("canplaythrough", () => {
    buttonLinks.forEach(btn => {
      btn.addEventListener("click", (e) => {
        setTimeout(() => {
          window.location = btn.getAttribute("href")
        }, audioEl.duration + delay);
      })
    });
  });
</script>   

What method can I follow for a more effective result here?

In the final version, the buttons sometimes sound and sometimes do not.

Thank you.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Here is an example that I threw together of clicking a button to play an MP3 in Javascript. I hope that this helps!

app.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sound Test</title>
</head>

<body>
    <button id="soundButton">Click Me!</button>
    <script type="text/javascript" src="app.js"></script>
</body>

</html>

app.js

const soundButton = document.querySelector('#soundButton');

soundButton.addEventListener('click', function () { playSound(); });

function playSound() {
    const sound = new Audio('catMeowing.mp3').play();
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda