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

282
Views
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 answers
Answer question

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